//构造中调用构造 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class Point{ public: Point(int _x,int _y,int _z){ cout << "自定义的有参构造函数被调用了1" << endl; x = _x; y = _y; z = _z; } Point(int a,int b){ cout << "自定义的有参构造函数被调用了2" << endl; x = a; y = b; //编译通过 运行通过 数据错误 Point(a, b, 10); //详解:调用有参构造函数Point(a, b, 10);会生成一个匿名对象,但是后续没有代码调用这个匿名对象 //所以匿名对象会立刻被析构 //与原来的对象没有任何关系 所以 原来的对象 成员属性 a,b赋值成功 z赋值不成功 } ~Point(){ cout << "自定义的析构函数被调用了3" << endl; } void GetNum(){ printf("pt的值是%d,%d,%d ",x,y,z); } private: int x; int y; int z; }; void ProtectA(){ Point pt(1,1); pt.GetNum(); //根据运行结果图也发现 构造函数被调用了2次 析构了2次 侧面说明了 匿名对象的存在 } void main(){ ProtectA(); system("pause"); }