#include<iostream>
using namespace std;
class test
{
public:
test(int a, int b, int c)
{
m_a = a;
m_b = b;
m_c = c;
cout << "我是构造函数1" << endl;
}
test(int a, int b)
{
m_a = a;
m_b = b;
test(a, b, 100);
cout << "我是构造函数2"<< endl;
}
~test()
{
cout << "我是析构函数"<< endl;
cout << m_a<<" "<<m_b<<" "<<m_c<< endl;
}
int getC()
{
return m_c;
}
int getA()
{
return m_a;
}
int getB()
{
return m_b;
}
private:
int m_a;
int m_b;
int m_c;
};
void main()
{
//调用构造函数2的时候构造函数2中调用的构造函数1会产生一个匿名对象
//且构造函数1被调用之后,会立即调用析构函数
//此时,c中会产生一个垃圾值
test test1(1, 2);
cout << "test.getA()="<<test1.getA()<< endl;
cout << "test.getB()=" << test1.getB() << endl;
cout << "test.getC()=" << test1.getC() << endl;
system("pause");
}
C++ 构造中调用构造——强化训练(三)
最新推荐文章于 2022-09-29 00:10:37 发布