If we dont have virtual constructors then why we have virtual destructors? Can constructors also be virtual?
Virtual destructors are needed because at destruction time, you don't always know what type you're dealing with:
Base *make_me_an_object()
{
if (the_moon_is_full())
return new Derived();
else
return new Base();
}
int main()
{
Base *p = make_me_an_object();
delete p;
}
The delete in the above program's main doesn't know whether its p points to a Base or a Derived object, but if the Base destructor is virtual (as it should be), then delete can use *p's vtable to find the right destructor.
By contrast, at construction time, you always know what kind of object you're creating. (And in case you don't, then you can create a factory or "virtual constructor" that does know.)