Question: I need to write a destructor for an exercise of school. I already tried to write the destructor for class A, is this the right or the wrong way?

[Exercise on paper](http://i.stack.imgur.com/V6gpk.jpg)

The exercise says the destructor needs to start like:

```
A *pa = .......;
delete pa;

```

The code of the 4 classes:

```
Class A
{
  private:
    vector <B*> b;
    vector <C*> c;
  public:
    ~A();
}

Class B
{
  private:
    vector <D*> d;
  public:
    ~B();
}

Class C
{
  private:
    vector <D*> d;
  public:
    ~C();
}

Class D
{
  private:
    vector <A*> a;
  public:
    ~D();
}

```

I've already tried to write the destructor for class `A`,
is this the correct way? 

```
~A()
{
  for (int i = 0; i < b.size(); i++)
  {
     B* pa = b[i];
     delete pa;
  }

  for (int j = 0; j < c.size(); i++)
  {
    C* pa = c[i];
    delete pa;
  }

```

Answer: 
Supposing you are not sure that the pointers in the two `vector`s in class `A` are all **not** `NULL`, then you can do the following:

```
~A()
{
  for (int i = 0; i < b.size(); i++)
    delete b[i];
  b.clear(); // Making sure you do not access to deleted pointers

  for (int i = 0; i < c.size(); i++)
    delete c[i];
  c.clear();  // Making sure you do not access to deleted pointers
}

```

We also clear the vectors, in order to be sure not to access to deleted pointes. Alternatively, if you still need the vectors' sizes, you can just set
`b[i] = nullptr` and `c[i] = nullptr` in the two loops, without clearing the vectors.
It depends. If `A` *owns* the pointers in the vectors and is therefore responsible for the destruction of the pointed objects and the objects were created using `new`, then yes the destructor does appear to be correct. 

If the pointed objects were not created with `new`, or the pointers are not *owned* by `A`, then `A`'s destructor should not delete them.

If `A` does own the pointers, it would be bad design, not to also allocate the objects within the class `A`. But I assume, that was left out of the example for simplicity.

As a sidenote: If `A` does own the pointers and deletes them in the destructor, then you should also implement the copy constructor and the copy assignment operator so that they make a deep copy or by making the class non-copyable. Otherwise you'll end up with undefined behaviour if you ever copy an instance.

> 
> But as you see in Class B and Class C I go to class D, but there would be an error if I delete D?
> 
> 
> 

Doing that is perfectly fine and there will be no error unless...

... if `A` owns the pointers to `B` and `C`, and `B` and `C` own the pointers to `D`, and `D` owns the pointers to `A`, so that they all delete the pointed objects, then there is an ownership cycle at class level. That's still fine unless...

... there is a cycle of pointers. For example, an instance of `D` (let's call it `dee`) points to an instance of `A` that points to an instance of `B` that points to `dee`, then you have a cycle and you end up deleting an object whose destructor has already started which results in undefined behaviour.

So, if such pointer cycle is possible, then the ownership cycle must be avoided. But if there can be no pointer cycle, then the ownership cycle may exist.