目录
4.2.3 拷贝构造函数的调用时机
C++中拷贝构造函数调用时机,通常有三种情况:
- 使用一个已经创建完毕的对象来初始化一个新对象。
- 值传递的方式给函数参数传值。
- 以值方式返回局部对象。
示例:
class Person
{
public:
Person()
{
cout << "Person 无参构造函数调用" << endl;
}
Person(int a)
{
age = a;
cout << "Person 有参构造函数调用" << endl;
}
Person(const Person& p)
{
age = p.age;
cout << "Person 拷贝构造函数调用" << endl;
}
~Person()
{
cout << "Person 析构函数调用" << endl;
}
int age;
};
// 1.使用一个已经创建完毕的对象来初始化一个新对象
void test01 ()
{
Person p1(20);
Person p2(p1);
cout << "p2的年龄: " << p2.age << endl;
}
// 2.值传递的方式给函数参数传值
void deal(Person p)
{
}
void test02()
{
Person p;
deal(p);
}
// 3.以值方式返回局部对象
Person deal2()
{
Person p;
cout << (int*)&p << endl;
return p;
}
void test03()
{
Person p = deal2();
cout << (int*)&p << endl;
}
int main() {
//test01();
//test02();
test03();
system("pause");
return 0;
}
test01();------------------------------------------------------------------
Person 有参构造函数调用
Person 拷贝构造函数调用
p2的年龄: 20
Person 析构函数调用
Person 析构函数调用
请按任意键继续. . .
test02();------------------------------------------------------------------
Person 无参构造函数调用
Person 拷贝构造函数调用
Person 析构函数调用
Person 析构函数调用
请按任意键继续. . .
test03();------------------------------------------------------------------
Person 无参构造函数调用
0073FA60
Person 拷贝构造函数调用
Person 析构函数调用
0073FB58
Person 析构函数调用
请按任意键继续. . .
4.2.4 构造函数调用规则
默认情况下,C++编译器至少给一个类添加3个函数:
- 1. 默认构造函数(无参,函数体为空)
- 2. 默认析构函数(无参,函数体为空)
- 3. 默认拷贝构造函数,对属性进行值拷贝
构造函数调用规则如下:
- 如果用户定义有参构造函数,C++不再提供默认无参构造,但是会提供默认拷贝构造
- 如果用户定义拷贝构造函数,C++不会再提供其他构造函数。
示例:
C++编译器会提供默认拷贝构造,对属性进行值拷贝。
class Person
{
public:
Person()
{
cout << "Person 无参构造函数调用" << endl;
}
Person(int a)
{
age = a;
cout << "Person 有参构造函数调用" << endl;
}
//Person(const Person& p)
//{
// age = p.age;
// cout << "Person 拷贝构造函数调用" << endl;
//}
~Person()
{
cout << "Person 析构函数调用" << endl;
}
int age;
};
// 构造函数的调用规则
// 1.如果用户定义有参构造函数,C++不再提供默认无参构造,但是会提供默认拷贝构造
void test01 ()
{
Person p1(20);
Person p2(p1);
cout << "p2的年龄: " << p2.age << endl;
}
int main() {
test01();
system("pause");
return 0;
}
------------------------------------------------------------------------------
Person 有参构造函数调用
p2的年龄: 20
Person 析构函数调用
Person 析构函数调用
请按任意键继续. . .
如果用户定义有参构造函数,C++不再提供默认无参构造。
class Person
{
public:
//Person()
//{
// cout << "Person 无参构造函数调用" << endl;
//}
Person(int a)
{
age = a;
cout << "Person 有参构造函数调用" << endl;
}
//Person(const Person& p)
//{
// age = p.age;
// cout << "Person 拷贝构造函数调用" << endl;
//}
~Person()
{
cout << "Person 析构函数调用" << endl;
}
int age;
};
// 构造函数的调用规则
// 1.如果用户定义有参构造函数,C++不再提供默认无参构造,但是会提供默认拷贝构造
void test01 ()
{
Person p; // 这句编译器会报错
Person p1(20);
Person p2(p1);
cout << "p2的年龄: " << p2.age << endl;
}
如果用户定义拷贝构造函数,C++不会再提供其他构造函数。
class Person
{
public:
//Person()
//{
// cout << "Person 无参构造函数调用" << endl;
//}
//Person(int a)
//{
// age = a;
// cout << "Person 有参构造函数调用" << endl;
//}
Person(const Person& p)
{
age = p.age;
cout << "Person 拷贝构造函数调用" << endl;
}
~Person()
{
cout << "Person 析构函数调用" << endl;
}
int age;
};
// 构造函数的调用规则
// 1.如果用户定义有参构造函数,C++不再提供默认无参构造,但是会提供默认拷贝构造
// 2.如果用户定义拷贝构造函数,C++不会再提供其他构造函数
void test01 ()
{
Person p; // 这句编译器会报错
Person p1(20); // 这句编译器会报错
Person p2(p1);
cout << "p2的年龄: " << p2.age << endl;
}
相关教程
- 开发环境搭建:Visual Studio 2019 C++开发环境搭建
- 推荐视频:https://www.bilibili.com/video/BV1et411b73Z?from=search&seid=4205594350351753444
- 已投币三连,非常细致的视频教程,感谢up主。