4.2.4构造函数调用规则
#include <iostream>
using namespace std;
class person4
{
public:
~person4()
{
cout << "析构构造" << endl;
}
person4(int age)
{
m_age = age;
cout << "有参构造" << endl;
}
person4(const person4 &p)
{
m_age = p.m_age;
cout << "拷贝构造" << endl;
}
int m_age;
};
void test04()
{
}
void test05()
{
person4 p(0);
}
int main4_2_4()
{
test04();
system("pause");
return 0;
}
4.2.3拷贝构造函数调用时机
#include <iostream>
using namespace std;
class person3
{
public:
person3()
{
cout << "默认构造" << endl;
}
person3(int age)
{
m_age = age;
cout << "有参构造" << endl;
}
person3(const person3 &p)
{
m_age = p.m_age;
cout << "拷贝" << endl;
}
~person3()
{
cout << "析构" << endl;
}
private:
int m_age;
};
void test01()
{
}
void dowork(person3 p)
{
}
void test02()
{
person3 p;
dowork(p);
}
person3 dowork2()
{
person3 p1;
return p1;
}
void test03()
{
person3 p = dowork2();
}
int main4_2_3()
{
test03();
system("pause");
return 0;
}