0

In a medium size code I have a base class like this

class Base {
public:
 virtual ~Base() = 0; //This is the important point
};

In a derivate class I have something like this

class Derivate : public Base {
public:
 ~Derivate();
};

An implementation of the virtual constructor is provided (it basically does some deallocation on the base class) the derivate class extends such behaviour to the derivate class. Since the base class is abstract I cannot instantiate anything.

I have a wrapper class like this

class Wrapper {
 public:
   ~Wrapper();
 private:
   Base *stuff;
}

Now I could have several derivates classes of the base one, so basically the purpose would be to let the user have the freedom of chose what to put in stuff The destructor of Wrapper is implemented as

Wrapper::~Wrapper() {
  stuff->~Base();
}

(My actual code follows such pattern). However when the destructor of wrapper is called an error is returned when I debug using gdb, where basically is stated that ~Base() Is a pure virtual method, which is true, however I thought that because is virtual anyway the actual constructor of the derivate class would have been called instead.

Is my understanding wrong? What Am I missing? Shall I decler the destructor of Base just as virtual and not pure virtual?

Note : Please keep in mind that the code I posted is a just some scratch.

Thank you.

user8469759
  • 2,522
  • 6
  • 26
  • 50
  • 5
    I don't get why you need to call the destructor manually. And you are probably missing a `delete stuff;` in `Wrapper::~Wrapper` instead, while I would recommend using `std::unique_ptr` instead of `Base *` as type for `stuff`. – Simon Kraemer Nov 14 '16 at 11:39
  • Is it just supposed to be declared and never called? there's the delete the Implementation of the destructor is correct, is the calling that puzzles me. – user8469759 Nov 14 '16 at 11:41
  • Your inderstanding is completely wrong. The `Wrapper` class accomplishes absolutely nothing, whatsoever. The `Base *` can simply be used directly, instead of `Wrapper`, in all situations. – Sam Varshavchik Nov 14 '16 at 11:45
  • @SamVarshavchik Why my understanding is wrong? please clarify. – user8469759 Nov 14 '16 at 11:48
  • As I said, the `Wrapper` class accomplishes absolutely nothing. Anywhere you think you want to use this `Wrapper` class, the `Base *` can simply be used directly. – Sam Varshavchik Nov 14 '16 at 11:49
  • @SamVarshavchik Wrapper classes are useful in several context, but that wasn't my question. – user8469759 Nov 14 '16 at 11:51
  • They may be useful in other contexts, but not in this one, where `Base *` can simply be used directly, with `delete stuff` destroying the object after it's no longer needed. – Sam Varshavchik Nov 14 '16 at 12:00
  • 1
    @SamVarshavchik And as I said I posted just some snippets for which I'd like to understand what actually happens, whether the wrapper is useful or not. – user8469759 Nov 14 '16 at 12:02
  • @SimonKraemer I've read the answer pointed at the beginning of my post. Is my understanding correct that If I allocate memory for a Wrapper object and then delete such memory then the destructor of Wrapper would be called, but in my case calling the destructor of base is wrong, I should instead just use delete of stuff object. Is that right? If yes I still don't understand why explicitly calling the destructor would be wrong. – user8469759 Nov 14 '16 at 13:14
  • @user8469759 there are some rare cases where you need to call a destructor manually but I highly doubt you are using one. What will happen most likely is that your destructor is called twice. If not handled correctly this might cause problems, especially if you expect it to be called only once. – Simon Kraemer Nov 14 '16 at 13:27
  • @SimonKraemer What about the other part of my understanding, was that correct? – user8469759 Nov 14 '16 at 13:38
  • @user8469759 Please see my first comment, but yes: calling delete will call the destructor. You should try it out by adding some debug output and/or a debugger. – Simon Kraemer Nov 14 '16 at 14:18

0 Answers0