Hi,
I find it would be helpful to have a friend function of a class someClass, like someClass operator + (someClass const& a, someClass const& b), to be inherited in all derived classes, so that I did not have to define it over and over again. How can I do that?
For the global operator+
(the binary form someClass operator+(const someClass&,const someClass&) )
You can't do it since the function is not a member of the class. The friend declaration is just giving it special
access to the internals of a particular class.
The unary form someClass someClass::operator+(const someClass&) is a member of the class and so could be made virtual.
In this case the operator+ called will depend on the left hand operand in the expression
(I haven't done this so there may be lurking problems in compound expressions with temporaries etc)
You need to write the bodies of all the operator+ functions anyway?
To avoid having lots of friend declarations you could make them non friends and supply public access functions to the stuff they need.
(this is often possible without having access functions you wouldn't want other class users to have - do you really need them to be friends?)
No they don't have to be friend functions. But this doesn't help me a lot, does it. someClass someClass::operator+(const someClass&) when I do a+b modifies a and I don't want that. That is why I want to use someClass operator + (someClass const& a, someClass const& b) instead. Unless there is another way?