2008-10-14

c++两点: 1用拷贝赋值运算符调用拷贝构造函数. 2.构造函数互相调用


class A
{
public:
A(int _x)
{
new(this)A(_x, 23);     //调用另外一个构造函数
}

A(int _x, int _y) { x = _x; y = _y; }

public:
int x;
int y;
};

class B
{
public:
B() { x = 4; }

B(const B& rhs)
{
if ( this == &rhs ) return;
x = rhs.x;
}

B& operator= (const B& rhs)
{
this->B::B(rhs); // 调用拷贝构造函数
}
public:
int x;
};

int _tmain(int argc, _TCHAR* argv[])
{
A a(3);

B b;
B b2(b);

cout << a.x << " " << a.y << endl;
cout << b2.x ;
return 0;
}