Virtual Destructors

Apr 5, 2014 at 11:34pm
Okay, so I'm a little confused on how to implement a destructor for a class which only has data members with automatic storage duration. Like, say you had these classes:

1
2
3
4
5
6
class A {
public:
   virtual ~A();
private:
   size_t p;
};


1
2
3
4
5
6
class B : public A {
public:
   virtual ~B();
private:
   char t;
};


How would you implement A::~A() and B::~B()? The only examples the notes and book give about explicitly defining constructors are when classes contain pointers to things on the heap, which doesn't apply here.

My best guess (and this is just a guess since I can't find anything out for sure) is to simply give the A::~A() and B::~B() empty bodies, but I'm really not sure. Clarification would be appreciated!
Last edited on Apr 5, 2014 at 11:35pm
Apr 6, 2014 at 12:06am
You would just make the destructors empty:
virtual ~A() {} // and similarly for B::~B

If you have a compiler that supports C++11 features, you could also do
virtual ~A() = default; // and similarly for B::~B
Apr 6, 2014 at 12:36am
I didn't know about the C++11 thing, but the other is basically what I thought. I just wasn't sure.

Thank you.
Topic archived. No new replies allowed.