Re: What is the correct way to derive a class in regard to overloaded operators
olanglois@sympatico.ca wrote:
I am trying to derive a new class that will add new functions
but no new data members and the base class has overloaded
operators (+,-,+=,-=,etc...) returning either (Base &) or
(const Base) depending on the operator:
class Derived : public Base
{
};
I'm not sure what you're doing is a good idea. Operator
overloading largely depends on value semantics, where as
derivation practically requires reference semantics to work
correctly.
The problem occurs if I do the following
Derived d1,d2;
Derived d3 = d1+d2;
what should be the better approach?
Just add
Derived(const Base&) and
Derived &operator=(const Base&)
or should it be better to redeclare all the operators (there
is a lot of them) in the derived class and perform nothing
except calling the base class version and return Derived
reference or object.
I'm not sure why you are deriving, so it's hard to say, but I
suspect that the cleanest approch would use containment, not
inheritance, and redefine every operator. It's a lot of copy
work, of course, but one way or the other, I don't see any way
around it.
--
James Kanze kanze.james@neuf.fr
Conseils en informatique orient9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S9mard, 78210 St.-Cyr-l'cole, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]