I have encountered the following problem which proved to me that I know far too little about the workings of C++.
I use a base class with pure virtual functions
class Base
...
and a derived classes of type
class Derived : public Base{
private:
Foo* f1;
...
Both have assignment operators implemented. Among other things, the assignment operator for Derived copies the data in f1. In my code, I create two new instances of class Derived
Base* d1 = new Derived();
Base* d2 = new Derived();
If I now call the assignment operator
*d1 = *d2;
the assignment operator of Derived is not called, and the data in f1 is not copied! It only works if I do
*dynamic_cast<Derived*>(d1) = *dynamic_cast<Derived*>(d2);
Can someone explain why the assignment operators are not overloaded?
Thanks!