-4

I have three classes

class A {
    // pure virtual funcs and member vars
    virtual ~A();
}

class B : public A {
    // some more pure virtual funcs
    virtual ~B();
}

class C : public B {
     // concrete implementations
     ~C() {}
}

Presently this doesn't compile with an 'undefined reference to `typeinfo' error (~B() is not defined, easily fixable) however I'm wondering if just defining 'virtual ~B {}' is the correct thing to do or whether ~C should be virtual and defined so calls to ~B are dispatched to ~C?

Madden
  • 1,024
  • 1
  • 9
  • 27

1 Answers1

0

Assuming that this question is about a pure virtual destructor (the code is not real, so it's difficult to say, but you're talking about a missing destructor definition, and about pure virtuals):

A pure virtual destructor that can be invoked, must be defined.

You can not define it in the class definition.

There is no clear reason why it must be defined external to the class definition, except an old comment by Bjarne Stroustrup (the language creator) that he viewed = 0 as indicating “no body”.


Example.

struct S
{
    virtual ~S() = 0;
};

S::~S() {}
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Why can't we just define it as an empty destructor in the class definition (of the abstract class `A`)? – dfrib Sep 19 '16 at 15:24
  • @πάνταῥεῖ: No, I meant in the class definition. Of the destructor. – Cheers and hth. - Alf Sep 19 '16 at 15:24
  • 1
    @dfri: If you define it as an empty destructor then it's no longer pure virtual. – Cheers and hth. - Alf Sep 19 '16 at 15:25
  • 1
    Where did you see the OP mentioning pure virtual destructors? – πάντα ῥεῖ Sep 19 '16 at 15:28
  • @Cheers and hth. - Alf: as πάντα ῥεῖ writes (no mention of pure virtual destructors; rather abstractness); aren't we consider an abstract class with (other) pure virtual methods? In which case we may supply an empty definition of the destructor while still keeping the class abstract. – dfrib Sep 19 '16 at 15:29
  • @dfri: Yes, of course. The assumption that the OP has an undefined pure virtual destructor may be wrong. He's talking about an undefined destructor, and about pure virtuals, but **those two aspects may be unrelated**, just a nonsense conflation on his part. – Cheers and hth. - Alf Sep 19 '16 at 15:31
  • @Cheersandhth.-Alf I see, and agree, thanks. – dfrib Sep 19 '16 at 15:37