c++ - Derived class inherit base class assignment operator? -
c++ - Derived class inherit base class assignment operator? -
it seems me derived class don't inherit base of operations class assignment operator if derived class inherit base of operations class assignment operator , can please explain next illustration
in next code overriding base of operations class operator= in derived, derived class default assignment operator calls overloaded operator=
#include <iostream> using namespace std; class base of operations { public: base(int lx = 0):x(lx) { } virtual base& operator=( const base of operations &rhs) { cout << "calling assignment operator in base" << endl; homecoming *this; } private: int x; }; class derived : public base of operations { public: derived(int lx, int ly): base(lx),y(ly) { } base& operator=(const base of operations &rhs) { cout << "assignment operator in derived"<< endl; homecoming *this; } private: int y; }; int main() { derived d1(10,20); derived d2(30,40); d1 = d2; }
it gives output
calling assignment operator in base
i have re-written base of operations class operator= derived class, if derived class inherits base of operations class operator= should overridden operator= (that have written in derived class), , derived class default operator= should phone call overridden version , not base of operations class operator=.
the compiler generates default assignment operator derived (which hides operator of base). however, default assignment operator calls assignment operators of class' members , base of operations classes.
c++ inheritance assignment-operator
Comments
Post a Comment