943,844 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2384
  • C++ RSS
Jul 28th, 2008
0

Why constructor can not be defined as virtual

Expand Post »
Why constructor can not be defined as virtual?

Thanks
Similar Threads
Reputation Points: 14
Solved Threads: 6
Light Poster
littlestone is offline Offline
42 posts
since Mar 2008
Jul 28th, 2008
0

Re: Why constructor can not be defined as virtual

google is a wonderful invention

Quote ...
A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.
http://www.codersource.net/published...ctions_in.aspx
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Jul 28th, 2008
0

Re: Why constructor can not be defined as virtual

Thank you so much.
Reputation Points: 14
Solved Threads: 6
Light Poster
littlestone is offline Offline
42 posts
since Mar 2008
Jul 29th, 2008
0

Re: Why constructor can not be defined as virtual

I think the only way to create a "virtual constructor" is to make a virtual method that returns the object inquestion, where derived classes return a copy of their type instead of the base classes return implementation.

By casting a derived type to a base type and invoking the virtual constructor method, you're constructing an object virtually, therefore it's safe to say it is a "virtual constructor".
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
May 30th, 2011
-2
Re: Why constructor can not be defined as virtual
constructor can't be defined as virtual bcuz if a class contains virtual function then the associate vtable must be initiated by the constructor of that class.if we define constructor as virtual then it creates a problem that who initiates that vtable.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avikmukherjee is offline Offline
1 posts
since May 2011
May 30th, 2011
0
Re: Why constructor can not be defined as virtual
Nice reply to 3-year-old thread. I suspect the OP already knows that by now.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005

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: Operator Parentheses Overload
Next Thread in C++ Forum Timeline: wxWidgets dialog problems





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


Follow us on Twitter


© 2011 DaniWeb® LLC