" Tiib" <oot...@hot.ee> wrote in message
news:7e2e42b3-2410-443a-95c2-46f262c8334b@n18g2000vbq.googlegroups.com...
On Feb 25, 11:07 am, "Paul" <pchris...@yahoo.co.uk> wrote:
Please let me know if you still do not understand and I will re-explain
again.
--Your code does not work. You can not overwrite object 'Dog dog;' in a
--way that Cat's destructor is called for it when that dog leaves
--scope ... Dog's destructor is called for it on all C++ compilers i
--could reach. What destructor will be called is pre-decided and set
--already compile time. So it seems that you do not understand yourself
--what you are talking about.
#include <iostream>
class Animal{};
class Dog: public Animal{};
class Cat: public Animal{};
int main()
{
Cat* p_cat = new Cat;
std::cout<< p_cat << std::endl;
delete p_cat;
Dog* p_dog = reinterpret_cast<Dog*>(p_cat);
p_dog = new Dog;
std::cout << p_dog;
}
Also ... most compilers optimize the two lines: