当前位置:网站首页>copy constructor
copy constructor
2022-04-23 06:37:00 【*Flowers bloom on the street】
Objects of the same class have complete structure in memory , If copying or copying as a whole is completely feasible . This copy process only needs to copy data members , And function members are shared ( There is only one copy ). When creating an object, another object of the same class can be used to initialize the storage space of the object , The constructor used in this case is called copy constructor .
When initializing one object with another , Will call the copy constructor .
If there is no copy constructor , The system will give a default copy constructor .
The following program creates several objects :( A total of 5 Objects )
#include<iostream>
using namespace std;
class Object
{
private:
int value;
public:
Object() { cout << "Object::Object " << this << endl; }// Default constructor
Object(int x) :value(x) { cout<<"Object::Object: " << this << endl; }// Constructor with parameters
~Object() { cout << "Object::~Object " << this << endl; }// Destructor
void SetValue(int x) { value = x; }
int Getvalue() const{ return value; }
Object(const Object &obj):value(obj.value)
{
cout << "Copy Creat" << endl;
}// copy constructor
};
Object fun(Object obj)//3.objx initialization obj, Create an object
{
int val = obj.Getvalue();
Object obja(val);//4. Building an object
return obja;// When function returns , Give a copy to a temporary quantity ( Dead value ),5. Build a dead value object
}
int main()
{
Object objx(0);//1. Create an object , Constructors
Object objy(0);//2. Create an object constructor
objy = fun(objx);// call fun() function
return 0;
}
Construct the dead value in the stack frame of the main function , If the function is built in the stack , function return After the end , There will be no dead value .
If you want to create fewer objects ,
1.2. Using a reference
Object fun(Object &obj)// If you don't want the reference to change the original object , Add const ,Object fun(const Object &obj)
{
int val = obj.Getvalue();
Object obja(val);
return obja;
}
2. Reference return
Reference return , Build dead values into functions .
版权声明
本文为[*Flowers bloom on the street]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230547206961.html
边栏推荐
- Robocode教程8——AdvancedRobot
- The most practical chrome plug-in
- Cf515b drazil and his happy friends
- Explanation of the second I interval of 2020 Niuke summer multi school training camp
- Object转Json差异之Gson fastJson jackson 修改字段名
- 进程间通信的方式
- Linux 用rpm的方式安装mysql(超简单)
- 【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (1)
- PM2 deploy nuxt project
- 静态成员
猜你喜欢
随机推荐
【UDS统一诊断服务】一、诊断概述(2)— 主要诊断协议(K线和CAN)
Common shortcut keys of IDE
scikit-learn sklearn 0.18 官方文档中文版
OpenCV使用 GenericIndex 进行 KNN 搜索
猜数字游戏
【UDS统一诊断服务】四、诊断典型服务(3)— 读故障信息功能单元(存储数据传输功能单元)
用二进制进行权限管理
拷贝构造函数
Dynamic creation and release, assignment and replication of objects
gcc ,g++,gdb的安装
基于pygame库编写的五子棋游戏
【UDS统一诊断服务】一、诊断概述(4)— 基本概念和术语
四元数乘法
多线程爬取马可波罗网供应商数据
爬取手游网站游戏详情和评论(MQ+多线程)
Cross domain issues - allow origin header contains multiple values but only one is allowed
Rust的闭包类型(Fn, FnMut, FnOne的区别)
搭建openstack平台
Wechat applet request encapsulation
【UDS统一诊断服务】三、应用层协议(1)