I’m trying to get my head around why I can’t overwrite a subclass object with another subclass object if they share base class.
Say that Letter is the base class. A and B are subclasses. The following doesn’t seem to work:
Letter* a_p = new A();
Letter* b_p = new B();
delete a_p;
*a_p = *b_p;
My ambition is to change what is located at a certain adress so that all pointers to the adress in question changes what they point to. In the above example, I’d like to somehow change the “content” of a_p to a copy of the “content” of b_p.
Is this possible somehow?
If you are doing a copy of the object and the type of the pointer is the base class, you lose all information about the subclasses.
And since you delete a_p, doing
*a_p=is wrong becausea_pis pointing to a memory not anymore allocated.Why not just doing :
a_p = b_p;?