why are friends used?
Nov 2, 2009 at 4:05am UTC
in overloading operators when set and get can be used in its place other than for performance reasons?
Nov 2, 2009 at 4:46am UTC
I think you miss the point of operator overloading.
Nov 2, 2009 at 5:22am UTC
can you share some light as to why that is the case?
Nov 2, 2009 at 5:36am UTC
I was thinking in my mind, if given any class, say for example:
1 2 3 4 5
class MyClass
{
int a;
double b;
};
how awkward it would be to add two objects of MyClass together using
get and
set
functions - rather than using function overloading and saying
1 2
MyClass a,b,c;
c = a+b
or something like:
1 2
MyClass a,b,c,d,e,f,g;
MyClass m = a+b+c+d+e+f+g
Or maybe I misunderstand the question. Maybe you can give an example using
set and
get functions.
Nov 2, 2009 at 6:07am UTC
I think he meant something like:
1 2 3
Obj& operator + (const Obj& o1, const Obj& o2) {
return Obj(o1.a()+o2.a());
}
Instead of friending the operator+ and just doing something like:
1 2 3
Obj& operator + (const Obj& o1, const Obj& o2) {
return Obj(o1._a+o2._a);
}
Nov 2, 2009 at 8:39am UTC
Topic archived. No new replies allowed.