Virtual constructors...
-
Why Virtual constructors are not allowed in C++??
-
Why Virtual constructors are not allowed in C++??
Because virtual constructor are simply useless ! When constructing an object, the compiler knows exactly which object it needs to create (as opposed to the destructors). Take the following example (CChild1 and CChild2 both derive from CBase):
CBase* pData = new CChild1; CBase* pData2 = new CChild2; // // ..... Code // delete pData; delete pData2;
If you look at the above example, when creating pData and pData2, the compiler knows explicitly the type of the object being created (new CChild1 or new CChild2). But when you delete the object, the compiler doesn't know it explicitly, thus requiring a virtual destructor. -
Why Virtual constructors are not allowed in C++??
Hello, The
virtual
keyword is used to say to the compiler that a pointer to an object might be a pointer to the base class instead of the class that was instantiated. This way the code looks in a virtual function pointer table for the right function to call. Since this mechanism is used to determine which member function should be called for the instance of a class, constructors cannot be virtual, since they create instances of classes. If virtual constructors would be allowed, I figure that it would never be possible to instantiate a class with a virtual constructor, once a class has been derived from it. This would be far from nice. Behind every great black man... ... is the police. - Conspiracy brother Blog[^] -
Hello, The
virtual
keyword is used to say to the compiler that a pointer to an object might be a pointer to the base class instead of the class that was instantiated. This way the code looks in a virtual function pointer table for the right function to call. Since this mechanism is used to determine which member function should be called for the instance of a class, constructors cannot be virtual, since they create instances of classes. If virtual constructors would be allowed, I figure that it would never be possible to instantiate a class with a virtual constructor, once a class has been derived from it. This would be far from nice. Behind every great black man... ... is the police. - Conspiracy brother Blog[^] -
Why Virtual constructors are not allowed in C++??
Objects are created from the inside out, and destroyed from outside in.
class A {
public:
A( void );
virtual ~A( void );
virtual void F( void );
long DA;
};class B : public A {
B( void );
virtual ~B( void );
virtual void F( void );
long DB;
};When you create an instance of A or B (or any class with a vtable) an pointer is placed at the start of the object. This pointer points to the class vtable. e.g.
A a; // [4 bytes - vtable ptr A][4 bytes - DA]
B b; // [4 bytes - vtable ptr B][4 bytes - DA][4 bytes - DB]When b is created: - memory is taken from the stack to hold size B object (12 bytes) - vtable ptr is set to A vtable - A::A() is called - vtable ptr is set to B vtable - B::B() is called Setting the vtable ptr and calling the approriate constructor method can be considered an atomic action from our point of view. From the above you can also see why you can't call virtual methods from a constructor and have them resolve forward - the vtable matches the class of the current constructor. e.g.
A::A( void ) {
F(); // ALWAYS calls A::F(), will never call B:F()
}This is part of the reason many frameworks adopt 2 part object construction: 1. create an instance, don't do anything important in constructor (init members) 2. call an Init() method that does the real construction - will resolve virtual methods properly When b is destroyed: - (vtable already set to B vtable) - B::~B() is called - vtable ptr is set to A vtable - A::~A() is called - memory is returned to stack From the above you can also see why you can't call virtual methods from a destructor and have them resolve forward - the vtable matches the class of the current destructor. e.g.
A::~A( void ) {
F(); // ALWAYS calls A::F(), will never call B:F()
}...cmk Save the whales - collect the whole set
-