0

My textbook says "Constructors cannot be virtual. (Think about it: constructors are called to create new objects. However, if you don't know what type of object you're trying to create, how do you know which constructor to invoke?)" but I am not sure what it means by this. Is it talking about constructors that dynamically allocate memory?

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by ArkM
0

i guess some properties of the vitrual functions like run - time binding arent there in constructors , Nevertheless syntactically they cannot created.

0

Yes but what is the reasoning behind not allowing it syntactically?

0

My textbook says "Constructors cannot be virtual. (Think about it: constructors are called to create new objects. However, if you don't know what type of object you're trying to create, how do you know which constructor to invoke?)" but I am not sure what it means by this. Is it talking about constructors that dynamically allocate memory?

>Is it talking about constructors that dynamically allocate memory?
Nope. It talking about any constructors.
Think again ;): virtual functions was invented for dynamic (in run-time) selection of the true (of derived class) object when you access it via base class pointer or reference.
If you call base class constructor (i.e. create object) you have no any other objects except this (now created) base class object. No need (and no sense) to search any other objects via virtual function mechanics.

So a virtual constructor is a senseless artifact. Try to reread the book again...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.