Archived

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

Do derived classes inherit deonstructors? C++

This topic is 5146 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
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
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
destructors can be inherited by sub classes, provided the superclass declares it virtual

constructors cannot be inherited by sub classes (try making your ctr virtual; the compiler will get pissed off), but the super class constructor will be called for you.

Share this post


Link to post
Share on other sites
Inherited isn't the correct terminology, though most people knew what the OP was asking.

dtors are automatically invoked in reverse order from the ctors.

... I think you can explicitly invoke a base dtor though, I guess they are inherited.


//Don't ever do this
struct Base
{
~Base()
{
cout << "wakka wakka\n";
}
};

struct Derived : Base
{};

Derived d;
d.Base::~Base();


[edited by - Magmai Kai Holmlor on November 10, 2003 11:23:58 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
Derived d;
d.Base::~Base();



It''s a Very Bad Idea, unless you promptly rebuild that part of the object with a placement new.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
i''m not particularly sure bout the inheriting of the base constructor aside from the default constructor. However, if u require explicit construction of a base class using its own overloaded constructors (with parameters), then the call to the base constructor must be explicit within the derived class constructors initializer list. eg:

class Base
{
public: Base(a, b) {}
}
class Derived : public Base
{
public: Derived() : Base(1, 2) {}
};


destructors on the other hand are usually only reliable in inheritance if u make them virtual. In fact, i read someone saying virtual destructors should have been automatic coz they''re pretty hopeless otherwise. comments?

Share this post


Link to post
Share on other sites
quote:
Original post by CraZeE
i''m not particularly sure bout the inheriting of the base constructor aside from the default constructor. However, if u require explicit construction of a base class using its own overloaded constructors (with parameters), then the call to the base constructor must be explicit within the derived class constructors initializer list. eg:

class Base
{
public: Base(a, b) {}
}
class Derived : public Base
{
public: Derived() : Base(1, 2) {}
};


destructors on the other hand are usually only reliable in inheritance if u make them virtual. In fact, i read someone saying virtual destructors should have been automatic coz they''re pretty hopeless otherwise. comments?

Actually, I have heard from my C++ teacher last spring about that topic, and his opinon on virtual destructors was about that. It makes sure that it will destroy the object cleanly that way, I think this is because this will call destructors all the way up the inheritence chain, from the lowest base class up to the one the object is of.

Share this post


Link to post
Share on other sites
Constructors are not inherited. Base class default constructors automatically get called, but you can override that behaviour by specifying the proper constructor to be used in an initialisation list. In particular, you have to do it if the base class has no default constructor.

As mentioned before, base class destructor automatically get called upon object destruction.

Trivia questions :

  • In which order do base classes get constructed ?
  • In which order do base classes get destroyed ?
  • What about multiple inheritance ?
  • What about virtual base classes ? (using virtual inheritance, not 'pure virtual' classes)

quote:
Original post by CraZeE
In fact, i read someone saying virtual destructors should have been automatic coz they're pretty hopeless otherwise. comments?


Your destructor only needs to be virtual if you are going to delete your object through a pointer to a base class. If you don't do that, you don't need a virtual destructor and, following C++'s philosophy, you shouldn't have to pay for what you don't use.

For example, if you inherit privately - being so used to adding the 'public' keyword before base classes, have you considered what 'protected' and 'private' inheritance meant ? - you can't use a pointer to the base class to manipulate derived objects at all (at least outside of the class), and therefore will not have any problems with a non-virtual destructor.

The suggestion has been made however, that the destructor should automatically be made virtual if you have another virtual function in the class , since it indicates that the class is designed for polymorphic use. This is perfectly fine in my book.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on November 10, 2003 11:57:27 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
quote:
Original post by Magmai Kai Holmlor
Derived d;
d.Base::~Base();
It''s a Very Bad Idea, unless you promptly rebuild that part of the object with a placement new.
Still a bad idea; the constructor might throw.

Share this post


Link to post
Share on other sites
Wow many, many posts

Constructors arn't inherited, but the base default constructor is always used in a derived class.

Is this correct?

[edited by - skow on November 11, 2003 4:37:11 PM]

Share this post


Link to post
Share on other sites
Er no. There might not be a default ctor, and the default ctor is not invoked if you explicitly invoke a different one. You can specify the default ctor in the initializer list, but this is redundant and not recommended.


[edited by - Magmai Kai Holmlor on November 11, 2003 4:49:43 PM]

Share this post


Link to post
Share on other sites