Why constructor can not be defined as virtual?

Thanks

Recommended Answers

google is a wonderful invention :)

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.

http://www.codersource.net/published/view/325/virtual_functions_in.aspx

Jump to Post

I think the only way to create a "virtual constructor" is to make a virtual method that returns the object inquestion, where derived classes return a copy of their type instead of the base classes return implementation.

By casting a derived type to a base type and invoking the …

Jump to Post

All 5 Replies

Thank you so much.

I think the only way to create a "virtual constructor" is to make a virtual method that returns the object inquestion, where derived classes return a copy of their type instead of the base classes return implementation.

By casting a derived type to a base type and invoking the virtual constructor method, you're constructing an object virtually, therefore it's safe to say it is a "virtual constructor".

constructor can't be defined as virtual bcuz if a class contains virtual function then the associate vtable must be initiated by the constructor of that class.if we define constructor as virtual then it creates a problem that who initiates that vtable.

Nice reply to 3-year-old thread. I suspect the OP already knows that by now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.