Friday, June 17, 2011

Why there is no virtual constructor in C++?

Answer: If a function is virtual, there needs to be a pointer to the virtual function table so that you can look up in the vtable. However, when the constructor is executed, the pointer to vtable is not there yet, so you can't be able to locate vtable. Besides, constructor is usually in-lined. If it is a virtual function, it can't be in-lined

No comments:

Post a Comment