Deleting structs from memory

I have a struct which inherits from 2 structs.

If I delete an instance of StructAB using a StructA pointer, will it also delete the contents of StructB?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct StructA {
  int A
};

struct StructB {
  int B
};

struct StructAB :public StructA, StructB {
};

int main() {
  StructA ** list = new StructA *[1];
  list[0] = new StructAB;
  delete StructA[0];
  delete[] StructA;
  //all memory released?
};


... What about in this case?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct StructA {
  int A
};

struct StructAB : public A {
  int B
};

int main() {
  StructA ** list = new StructA *[1];
  list[0] = new StructAB;
  delete StructA[0];
  delete[] StructA;
  //all memory released?
};

Last edited on
This code makes no sense, AB doesn't inherit form A, so a pointer to A can't be used to point to an object AB.

In the case that you simply forgot this, then Google "virtual destructor".

Basically, make the destructor virtual, so that the highest destructor in the hierarchy gets called.
I did forget it, thanks.

I have now edited the above example.
I think this is it.

THIS is where my memory has been going!!!!!



I could kiss you
Topic archived. No new replies allowed.