Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 21st, 2005, 4:22 AM   #1
kaustubhkane
Newbie
 
Join Date: Sep 2005
Posts: 2
Rep Power: 0 kaustubhkane is on a distinguished road
Why can't we have Virtual Constructors???

Hi All,

While using the concept of Polymorphism in C++ we use Virtual Destructors. These are normally used when we maintain a common list (normally array of pointers of type Base Class) and each element in this array can point to the derived class... for e.g:

Base* ptr[i] = new Derived;

While destroying the objects we use Virtual destructors.

In the same way why cant we have Virtual Constructors???

Eagerly waiting 4 the reply...

Thanks..
kaustubhkane is offline   Reply With Quote
Old Sep 21st, 2005, 6:29 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
It's been a while since I've programmed in C++, but as I understand it, the difference between virtual and non-virtual functions is that non-virtual functions are chosen dependant on the casted type of an object, whereas virtual fuctions are chosen dependant on the internal type of an object.

So without the virtual keyword, the command "((Animal)dog).speak()" will call a different function from "dog.speak()".

Because of this, there is no such thing as a virtual constructor, because a constructor cannot be cast, because the object hasn't been created yet. So a virtual constructor makes no sense.
Arevos is offline   Reply With Quote
Old Sep 21st, 2005, 6:33 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
One way of looking at it is that a virtual destructor destroys the most heavily derived object. Exactly how are you going to map that action to construction?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 21st, 2005, 7:49 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
The actual reason is that the choice of calling a virtual function is based on the actual type of an object. The order of construction (base classes constructors are invoked before derived class constructors) means that the type is not determined until after the most derived constructor is invoked (i.e. after the object is constructed).

The term "virtual constructor", in the sense that you mean it, is therefore actually a contradiction: a member function of a class can either be virtual or it can be a constructor. It cannot be both.
grumpy is offline   Reply With Quote
Old Sep 21st, 2005, 9:21 AM   #5
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
Here's the reason straight from Mr. Stroustrup himself:
http://www.research.att.com/~bs/bs_f...l#virtual-ctor
Scorpions4ever is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC