Operator assignment function help!

Is it possible to create a function outside of the main, that is not part of a class, just as a solo helper function that can overload operators?

I know that it is possible as a class member, for example:

class Fraction
{
.....
friend Fraction operator + (Fraction p, Fraction q)
....
}

Is it possible to make this operator function outside of a class? For example, I want to find the sum of 2 integer variables. I know the simplest solution is just, int x + int y, but is it possible to call a function to do this?

I'm not exactly sure what you are asking. you can do
1
2
3
4
5
6
7
8
9
10
int addition(int x, int y); //this makes sure your program can find the addition function
                                    //and can be added into a header file

int main(){
    int var = addition(5, 80); //calling the function
    return 0;
}
int addition(int x, int y){ //this is a function
    return x+y;
}

you cannot overload an operator that is not in a class.
1
2
3
4
5
6
7
int operator+(int a){ //error, not in a class
	return a;
}

int main(){
       return 1+-1;
}

Last edited on
Ah ok that was what I was asking for, cheers for clarifying!
Topic archived. No new replies allowed.