Ok, that's why I've never thought about this. I don't think I've ever used a base pointer to delete a derived object. Coding
is not something I normally do.
But since it is possible the designers of C++ should have made all destructors implicitly virtual since delete knows absolutely nothing about the type of object that it is deleting. Also this would not effect those situations where virtual was not needed but it would correct the problem that occurs when it is.
Seems like an oversight to me. But like I said - personally I try to avoid creating objects in this way. My normal method is this:
Code:
B* obj=new B();
D* obj=new D();
Why would you want to access a derived class's constructor from a base pointer?? Why would you use a base pointer to create a derived object?
Seems unecessarily complex and 'jiggy' to me.
Code:
class Object
{
public:
Object(void);
virtual ~Object(void);
};
class Square, public Object
{
public:
Square(void);
~Square(void);
};
Object *obj=new Square();
If you wanted a Square object, why not just do:
Code:
Square *obj=new Square();
What are the benefits of:
Code:
Object *obj=new Square()
??