Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

baskuenen

Virtual constructors ???

This topic is 6926 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey people, No blabla, here''s what I want: class VEHICLE { public: VEHICLE(int iWheels); }; class CAR: public VEHICLE { public: CAR(); }; CAR::CAR() { // init stuff inherited::VEHICLE(4); // init more stuff } It''s getting late and my C++ is getting a bit rusty, how do I solve this? Hope the code above shows I want to call my own inherited contructor. Do I need to make this contructor VIRTUAL??? Or is it bed time?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
There is no such thing as a virtual constructor - but here is how you do what you want todo

CAR::CAR() : VEHICLE(4)
{
// init car stuff
}

Share this post


Link to post
Share on other sites
ECKILLER,
Some other programming languages use virtual constructors. Delphi and probably C++ Builder also?

Anonymous,
I see I''ve lost too much brain cells already, and C++ with them.

Thanks people...coding can continue...

Share this post


Link to post
Share on other sites
C++ Builder does not use virtual constructors.

You can simulate virtual constructors by using a factory method pattern.

That is, if what you really want to do is construct different objects returned as a pointer to a common base class.

But what you're doing in the example is just a normal thing.

All the constructors of an object and the classes that it inherits from are called, in order, from the most base class to the most derived, when you do a new Car or declare a stack ( automatic ) variable of the type.


Edited by - SteveC on May 8, 2000 10:46:16 PM

Share this post


Link to post
Share on other sites
Nope. Cam''t override constructors. Period.

PCMCIA - People Can't Memorize Computer Industry Acronyms
ISDN - It Still Does Nothing
APPLE - Arrogance Produces Profit-Losing Entity
SCSI - System Can't See It
DOS - Defunct Operating System
BASIC - Bill's Attempt to Seize Industry Control
IBM - I Blame Microsoft
DEC - Do Expect Cuts
CD-ROM - Consumer Device, Rendered Obsolete in Months
OS/2 - Obsolete Soon, Too.
WWW - World Wide Wait
MACINTOSH - Most Applications Crash; If Not, The Operating System Hangs

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!