Class Inheritance and Constructors/Destructors

Started by
12 comments, last by Jason2Jason 21 years, 7 months ago
quote:Original post by biovenger
No, the derived constructor gets called first, then the default base constructor (BaseClass(); {}).


You''re wrong. Have you even tried this?

Here''s some proof code.


  #include <iostream>class CBase{        public:        CBase() { std::cout << "CBase::CBase()\n"; }};class CDerived : public CBase{        public:        CDerived() { std::cout << "CDerived::CDerived()\n"; }};int main(){        CDerived myClass;        return 0;}  


Gosh, it runs the base-class ctor first! fancy that!


Don''t listen to me. I''ve had too much coffee.
Advertisement
HELLOOOOO!!!!! has anybody paid any attention to my first post? I think it has illustrated it pretty conclusively, why are you still discussing this?

Gyzmo
==============================
"Well is the world standard" - The Alchemist
"Not in Canada!" - jonnyfish
Gyzmo=============================="Well is the world standard" - The Alchemist"Not in Canada!" - jonnyfish
yes i did, it works as you said, so if any one says other wise, they are wrong.

-J
If Gyzmo didn''t clear it up for you all, just write a small test app for yourselves.

// Create an object here, and the compiler will call these for you, in this order
CBase ()
CDerived ()
// It goes out of scope or gets deleted, the compiler will call these for you, in this order
~CDerived ()
~CBase ()

You should never have to call a destructor manually. You normally never have to call an constructor manually.

Also, always make your base class destructor virtual. Otherwise the derived one will not be called when you do this:

Base * p = new Derived;
delete p;

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

This topic is closed to new replies.

Advertisement