/*
构造中调用构造是危险的行为
*/
#if 1
class MyTest
{
public:
MyTest(int a, int b, int c)
{
this->a = a;
this->b = b;
this->c = c;
}
MyTest(int a, int b)
{
this->a = a;
this->b = b;
//给c赋值
MyTest(a, b, 100); //直接调用构造函数,产生新的匿名对象,没人接,
//会直接调用匿名对象的析构函数。 跟t1对象没有半毛钱关系
}
~MyTest()
{
printf("析构函数 MyTest~:%d, %d, %d\n", a, b, c);
}
protected:
private:
int a;
int b;
int c;
public:
int getC() const { return c; }
void setC(int val) { c = val; }
};
void test()
{
MyTest t1(1, 2);
printf("-----c:%d", t1.getC()); //请问c的值是?竟然是乱码
}
#endif