Jump to content
  • Advertisement

Archived

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

skow

Do derived classes inherit deonstructors? C++

This topic is 5337 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

I have a pure virtual base class that i derive many sub classes off of. I can't seem to find this in any of my books, and in a quick google search. Do derived classes inherit deonstructors, or constructors for that matter? I would think yes but as the sonstructor/deconstructor have names dependent on the class's name, so I have doubt. Any quick anwser is apricated. Edit: I'm using c++ [edited by - skow on November 9, 2003 3:07:58 PM]

Share this post


Link to post
Share on other sites
Advertisement
yes, they do. A class''s base class destructors are automatically called after its own destructor returns.

Keep in mind, tho, that you will have to declare your base class destructor virtual, if you will ever be calling delete on a base class pointer.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
yes, they do. A class''s base class destructors are automatically called after its own destructor returns.

"Sneftel is correct, if rather vulgar." --Flarelocke


Great! Thanks!

Share this post


Link to post
Share on other sites
Yes.

you can try this (and other stuff) like:


class A
{
public:
A()
{
printf("A ctor");
}
~A()
{
printf("A dtor");
}
};
class B: public A
{
public:
B()
{
printf("B ctor");
}
~B()
{
printf("B dtor");
}
};

main
{
A a;
B b;

B *pB = new B;
A *pA = pB;

delete pA;
}


This is not exactly what you''re asking, but I bet this interrest you!
Notice the new''ed class doesnt completely get destructed!
Making the destructor virtual helps.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
WHAT!?!?

A derived class does not inherit constructors and destructors from the base class. Consider this:

class Base
{
public:
Base(int i) {};
};

class Derived : public Base {};

// The following will not work, because Base::Base(int) is NOT inherited

Derived derived(5);

See, constructors (and destructors for that matter) are NOT inherited. Don''t listen to the above posters.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
don''t listen to the above poster.

Share this post


Link to post
Share on other sites
If you create Derived, the constrctor of Base which has no arguments are called. If you want to inherit from a class which has arguments in its constructor you must do it like this:

class Base{
public:
Base(int i){};
};

class Derived : public Base {
public:
Derived(int i) : Base(i){};
};


Derived derived(5);


"On a long enough timeline the survival rate of everyone drops to zero"
- Fight Club

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by emileej
If you create Derived, the constrctor of Base which has no arguments are called. If you want to inherit from a class which has arguments in its constructor you must do it like this:

class Base{
public:
Base(int i){};
};

class Derived : public Base {
public:
Derived(int i) : Base(i){};
};


Derived derived(5);


"On a long enough timeline the survival rate of everyone drops to zero"
- Fight Club

Aha, but you have to explicitly declare the constructor in the derived class, therefore it isn''t inherited...

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
quote:
Original post by emileej
If you create Derived, the constrctor of Base which has no arguments are called. If you want to inherit from a class which has arguments in its constructor you must do it like this:

class Base{
public:
Base(int i){};
};

class Derived : public Base {
public:
Derived(int i) : Base(i){};
};


Derived derived(5);


"On a long enough timeline the survival rate of everyone drops to zero"
- Fight Club

Aha, but you have to explicitly declare the constructor in the derived class, therefore it isn''t inherited...


Only because it doesn''t know what values you want to pass it. Contrary to popular beleif, compilers do NOT have ESP.

Also, the example above was incorrect from "baskuenen", please make the deconstructor virtual in class A.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!