Re: Multiple inheritance and pointer equivalence
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4b0f84ec$0$10444$4fafbaef@reader2.news.tin.it...
"Danny Woods" <dannywoodz@yahoo.co.uk> ha scritto nel messaggio
news:50skccrsex.fsf@gmail.com...
Hi all,
Given this simple program:
----
#include <cstdio>
class A { public: virtual ~A() {} };
class B { public: virtual ~B() {} };
class C : public A, public B { public: virtual ~C() {} };
int main(void)
{
C *c = new C();
A *a = c;
B *b = c;
printf("c: %p; a: %p; b: %p\n", c, a, b);
delete c;
return 0;
}
----
i not find in the example above no double free;
where is it?
where is the problem?
the debugger show that the call for construction
are C()A()B()
and for distruction ~C()~B()~A()
(this mean that printf print someting wrong here below)
where is the problem?
A()B()C()
c: 00852FC0; a: 00852FC0; b: 00852FC8
~C(); this=00852FC0
~B(); this=00852FC8
~A(); this=00852FC0
END
------------------------
#include <stdio.h>
#include <stdlib.h>
#define P printf
#define i8 signed char
#define V void
class A{
public:
A(){ Aarr= (i8*) malloc(1024); P("A()"); }
virtual ~A()
{P("~A(); this=%p\n", (V*)this);
free(Aarr);
Aarr=0;
}
i8* Aarr;
};
class B{
public:
B(){ Barr= (i8*) malloc(1024); P("B()"); }
virtual ~B()
{P("~B(); this=%p\n", (V*)this);
free(Barr);
Barr=0;
}
i8* Barr;
};
class C : public A, public B{
public:
C(){P("C()");}
virtual ~C(){ P("~C(); this=%p\n", this); }
};
int main(void)
{
C *c = new C;
A *a = c;
B *b = c;
if(c->Aarr==0||c->Barr==0)
{P("No memory\n");
goto end;
}
printf("\nc: %p; a: %p; b: %p\n", (V*)c, (V*)a, (V*)b);
end:;
delete c;
P("END\n");
return 0;
}