1 Star 0 Fork 0

CoderQi/cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
拷贝构造函数的调用时机.cpp 1.36 KB
一键复制 编辑 原始数据 按行查看 历史
CoderQi 提交于 2023-03-21 21:57 +08:00 . 23_321
#include<iostream>
using namespace std;
//class Person
//{
//public:
// Person()
// {
// cout << "Person默认构造函数已调用" << endl;
// }
// Person(int age)
// {
// cout << "Person有参构造函数已调用" << endl;
// m_Age = age;
// }
// Person(const Person &p)
// {
// cout << "Person拷贝构造函数已调用" << endl;
// m_Age = p.m_Age;
// }
// ~Person()
// {
// cout << "Person析构函数已调用" << endl;
// }
// int m_Age;
//};
////1.使用一个已经创建完毕的对象来初始化一个新对象
//void test01()
//{
// Person p1(10);
// Person p2(p1);
// cout << "p2的年龄是:" << p2.m_Age << endl;
//}
////2.值传递的方式给函数参数传值
//void doWork(Person p)
//{
//
//}
//
//void test02()
//{
// Person p;//调用了默认构造函数
// doWork(p);//在值传递时(临时拷贝数据时)调用了拷贝构造函数
//}
//
////3.值方式返回局部对象
//Person doWork2()
//{
// Person p1;
// cout << (int*)&p1 << endl;
// return p1;//返回值时调用了拷贝构造函数
//
//}
//void test03()
//{
// Person p=doWork2();
// cout << (int*)&p << endl;
//
//}
//
//int main()
//{
// //test01();
// //test02();
// test03();
//
// return 0;
//}
class Printer {
public:
Printer(std::string name) { std::cout << name; }
};
class Container {
public:
Container() : b("b"), a("a") {}
Printer a;
Printer b;
};
int main() {
Container c;
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/programmercg/cpp.git
git@gitee.com:programmercg/cpp.git
programmercg
cpp
cpp
master

搜索帮助