S
syntheticpp
Is the program at the end a correct C++ program?
I ask because the output is:
B:perator A&()
A::A()
B::B()
B::~B()
A::~A()
that means the casting operator which sets the
member variable B: is called before the constructor
of B.
Here the program:
#include <iostream>
using std::cout;
struct A
{
A(){ cout <<"A::A()\n"; }
~A(){ cout <<"A::~A()\n"; }
};
struct B
{
B() // : p(0) // this kills the desruction of p
{ cout <<"B::B()\n"; }
~B()
{
cout <<"B::~B()\n";
delete p;
}
A* p;
operator A&()
{
cout <<"B:perator A&()\n";
p = new A;
return *p;
}
};
struct C
{
A& a;
B b;
C() : a(b) {}
};
int main()
{
C* c = new C;
delete c;
#if defined(__BORLANDC__) || defined(_MSC_VER)
system("PAUSE");
#endif
}
I ask because the output is:
B:perator A&()
A::A()
B::B()
B::~B()
A::~A()
that means the casting operator which sets the
member variable B: is called before the constructor
of B.
Here the program:
#include <iostream>
using std::cout;
struct A
{
A(){ cout <<"A::A()\n"; }
~A(){ cout <<"A::~A()\n"; }
};
struct B
{
B() // : p(0) // this kills the desruction of p
{ cout <<"B::B()\n"; }
~B()
{
cout <<"B::~B()\n";
delete p;
}
A* p;
operator A&()
{
cout <<"B:perator A&()\n";
p = new A;
return *p;
}
};
struct C
{
A& a;
B b;
C() : a(b) {}
};
int main()
{
C* c = new C;
delete c;
#if defined(__BORLANDC__) || defined(_MSC_VER)
system("PAUSE");
#endif
}