943,866 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 420
  • C++ RSS
Mar 29th, 2009
0

Question about virtual functions

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Mar 29th, 2009
0

Re: Question about virtual functions

i guess some properties of the vitrual functions like run - time binding arent there in constructors , Nevertheless syntactically they cannot created.
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Mar 29th, 2009
0

Re: Question about virtual functions

Yes but what is the reasoning behind not allowing it syntactically?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Mar 29th, 2009
0

Re: Question about virtual functions

Click to Expand / Collapse  Quote originally posted by CPPRULZ ...
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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Question on const Foo *const
Next Thread in C++ Forum Timeline: C++ "Access violation reading location 0x00000021."





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC