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?

Recommended Answers

All 3 Replies

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

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

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...

Be a part of the DaniWeb community

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