It seems to me that Derived class don't inherit base class Assignment operator
if Derived class inherit Base class assignment operator , can you please explain the following example
In the following code I am overriding base class operator= in Derived, so that Derived class default assignment operator calls overloaded operator=
#include <iostream>
using namespace std;
class Base
{
public:
Base(int lx = 0):x(lx)
{
}
virtual Base& operator=( const Base &rhs)
{
cout << "calling Assignment operator in Base" << endl;
return *this;
}
private:
int x;
};
class Derived : public Base
{
public:
Derived(int lx, int ly): Base(lx),y(ly)
{
}
Base& operator=(const Base &rhs)
{
cout << "Assignment operator in Derived"<< endl;
return *this;
}
private:
int y;
};
int main()
{
Derived d1(10,20);
Derived d2(30,40);
d1 = d2;
}
It gives the output
calling Assignment operator in Base
I have re-written base class operator= into derived class, so if derived class inherits base class operator= then it should be get overridden by operator= (that i have written in derived class), and now Derived class default operator= should call overridden version and not from the base class operator=.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…