1

After reading this post Use-cases of pure virtual functions with body? I am under the impression that it is possible to avoid using a pure virtual function with a body however I can't figure out how to do it in this case:

I have an abstract base class that I'll call "parent" and a collection of derived class that I'll call "child1", "child2" etc. I also have another class that I'll call "list" that contains an array of parent pointers that can be set to point to instances of various collections of children. For example, the array field of an instance of a list may contain pointers to 2 child1's and 1 child2.

When deleting an instance of list I'd like to free the various instances of child1's and child2's thus I need a destructor for child1,child2 etc. Additionally this requires me to have a destructor for the abstract parent class.

Again, the link above has persuaded me that I can find a way to not do this, or possible, that my problem is poorly conceived because I have found myself needing to do this.

If stack overflow is the wrong stackexchange for this question please let me know and I'll move it.

Thanks for the help PS: here is some code:

#include <iostream>
#define arlen 10

class parent{
  public:
  virtual ~parent();
  void somefunction();
};

parent::~parent(){}

class child1: public parent{
  public:
  ~child1();
  void somefunction();
};

child1::~child1(){}


class child2: public parent{
  public:
  ~child2();
  void somefunction();
};

child2::~child2(){}

class list{
  public:
  parent* ar[arlen];
  ~list(){
    for (int n=0; n<arlen; n++) delete ar[n];
  }
}

int main(){}
Mathew
  • 1,116
  • 5
  • 27
  • 59

1 Answers1

2

Sounds to me like all you really need is a virtual destructor for parent.

With a virtual dtor, deleting via a parent pointer the compiler will correctly call the derived class destructor.

It that's all you actually need and I understood your question correctly, then adding

 virtual ~parent() = default;

to your base class should do the trick.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • Does doing that make the class abstract? –  May 14 '18 at 20:15
  • @NeilButterworth I hope not, why should it? – 463035818_is_not_an_ai May 14 '18 at 20:16
  • 1
    @Neil Butterworth - As you very well know; No. But OP did say "I have an abstract base class ...", so I assume there are other pure virtual member functions in the class, rendering it abstract. – Jesper Juhl May 14 '18 at 20:18
  • yes, the base class will defitely be abstract, there will be other virtual functions in it – Mathew May 14 '18 at 20:20
  • @JesperJuhl, thanks, I think what you suggested using default is what I want, however I don't have c++11 so I'll just stick to using a body for the virtual function. Thanks again – Mathew May 14 '18 at 20:27
  • 1
    @fred there is nothing wrong about virtual methods that have a body in an abstract class. One pure virtual method is enough to render the class abstract. I know little about OO theory, so it might be that some evangelists have different views, but who cares? :P – 463035818_is_not_an_ai May 14 '18 at 20:40