Question about virtual functions

Reply

Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Question about virtual functions

 
0
  #1
Mar 28th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 262
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 16
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: Question about virtual functions

 
0
  #2
Mar 29th, 2009
i guess some properties of the vitrual functions like run - time binding arent there in constructors , Nevertheless syntactically they cannot created.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: Question about virtual functions

 
0
  #3
Mar 29th, 2009
Yes but what is the reasoning behind not allowing it syntactically?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 344
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Question about virtual functions

 
0
  #4
Mar 29th, 2009
Originally Posted by CPPRULZ View 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?
>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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 321 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC