This is just a test project to understand how inheritance works. Cat is a subclass of Mammal, which is again a subclass of Animal.
int main() { Cat* cat1 = new Cat("nosy grey", 1.0d, 3); Cat* cat2 = new Cat("purply green", 2.0d, 4); Cat* cats[] = {cat1, cat2}; delete [] cats; }
So I canβt do it, because then I get it.
*** Error in `/home/max/git/info-2-ss/Blatt3/Aufgabe2/main.exe': double free or corruption (out): 0x00007fff55fd7b10 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x80a46)[0x7f3a07452a46] /home/max/git/info-2-ss/Blatt3/Aufgabe2/main.exe[0x40178e] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7f3a073f3ea5] /home/max/git/info-2-ss/Blatt3/Aufgabe2/main.exe[0x400d39]
I exit when my constructors and destructors are called, so when my Cats are created, I get something like this:
Called ctor of Animal with age: 3 Called ctor of Mammal with hairLength: 1 Called ctor of Cat with eyecolor: nosy grey
When I change my code a bit, so it reads:
delete [] *cats;
then I would expect my dtors to be called like this for every Cat:
Called dtor of Cat Called dtor of Mammal Called dtor of Animal
instead, I get this single line:
Called dtor of Cat
Summary: How can I effectively remove my arrays so that all my dtors receive a call?
source share