using namespace std;
class A {};
class B {};
class C : public A {
public:
C(){ b = new B();}
B* b;
~C(){
printf("in destructor\n");
delete b;
}
};
void doSomething(A&aref)
{
C c = (C &) aref;
c.b = new B();
int i;
}
int main()
{
A * a;
a = new C();
printf("Line 1\n");
doSomething(*a);
printf("Line 2\n");
delete(a);
return 0;
}
Output is:
Line 1
in destructor
Line 2
Tried removing the delete(a)
and get same results.
Why don't I see the destructor called twice? I'd expect it to be called at the end of the doSomething
function and at the delete
call.
Why does the doSomething
function call the destructor? And why does the delete(a)
not cause an error?