当前位置:网站首页>Copy (copy) constructors and assignment overloaded operators=
Copy (copy) constructors and assignment overloaded operators=
2022-06-13 01:21:00 【Zhao_| adult】
List of articles
1. copy constructor
#include <iostream>
#include <list>
#include <vector>
#include <random>
using namespace std;
class Construct{
public:
Construct(){
std::cout<<" Constructors :Construct()"<<std::endl;
};
Construct(int data):m_data(data){
std::cout<<" Constructors :Construct(int &data)-->"<<m_data<<std::endl;
}
Construct(const Construct &cons):m_data(cons.m_data){
std::cout<<" copy constructor : Construct(const Construct &cons)-->"<<m_data<<std::endl;
}
Construct & operator=(const Construct & cons){
m_data=cons.m_data;
std::cout<<" Overload assignment operator =:Construct & operator=(const Construct & cons)"<<std::endl;
return *this;
}
~Construct(){
std::cout<<" Destructor :~Construct()"<<endl;
}
private:
int m_data=0;
};
int main()
{
Construct a(50); // Call constructor :Construct(int data);
Construct b(a); // Call the copy constructor :Construct(const Construct &cons);
Construct c=b; // Call the copy constructor :Construct(const Construct &cons)? Here's why :
//! The purpose of the constructor is to allocate memory space to objects , And new objects c Memory space has not been allocated , Call at this time b Assign a value to , will
//! Call the copy constructor , While allocating space , Copy data ;
c=a; // call Construct & operator=, because c You have already initialized and allocated storage space , At this time, it is for an existing
// Object to assign , Will call the assignment overloaded function ;
Construct *d=nullptr; //! No constructor will be called , because d It's a pointer , It just points to a Construct object ;
Construct *e=new Construct; //! Call constructor :Construct(int data);
return 0;
}
//-----------------------------------------------------------------------------------
Constructors :Construct(int &data)-->50
copy constructor : Construct(const Construct &cons)-->50
copy constructor : Construct(const Construct &cons)-->50
Overload assignment operator =:Construct & operator=(const Construct & cons)
Constructors :Construct()
Destructor :~Construct()
Destructor :~Construct()
Destructor :~Construct()
The copy constructor is Only when the object is instantiated Will be called , in other words , A new object is generated ( Allocate storage space ), Will call the constructor ;
The assignment operator is in An existing object Called when a new value is assigned , And it has a return value .
In the above example :
Construct a(50);
Construct b(a);
Construct c=b;
a,b,c Is a newly created object , Not before a,b,c, At this point, you need to call the constructor to allocate memory space and initialize ; So the constructor will be called , The call constructor is the default constructor Construct(int &data)/Construct(); The copy constructor depends on the parameters passed ( heavy load );
c=a;
and c=a in , object c It's an existing object , Its memory space has been allocated , So it won't call copy constructor , Instead, the assignment is made in the way of assignment ( heavy load =);
2. summary
So it's not : Construct b(a); Just call copy constructor , and Construct c=b; Just call Assignment operator ; Instead, it is called according to the properties of the assignment object , If it is a new object , At this point, the constructor must be called ( Default constructor / copy constructor ) Allocate memory space and initialize ;
For existing objects ( Memory space has been allocated ) Will perform The assignment operation ;
边栏推荐
- The tle4253gs is a monolithic integrated low dropout tracking regulator in a small pg-dso-8 package.
- Go simple read database
- 304. Merge two ordered arrays
- Stack and queue practice (C language): Demon King's language
- leetcode. 151. flip the words in the string
- Quantitative investment traditional index investment decision vs Monte Carlo simulation method
- Leetcode-19- delete the penultimate node of the linked list (medium)
- Calculate sentence confusion ppl using Bert and gpt-2
- [Latex] 插入圖片
- Alexnet implements image classification of caltech101 dataset (pytorch Implementation)
猜你喜欢

五篇经典好文,值得一看(2)

Jenkins持续集成操作

Quantitative investment traditional index investment decision vs Monte Carlo simulation method

Realization of flip animation

Create a simple game interface using pyGame

Database query user mailbox

Go JWT learning summary

Addition and modification of JPA

Page optimization - Notes

How to print infinite symbol in WPS
随机推荐
Tkinter library installation
About_ int128
Go simple read database
Et5.0 configuring Excel
Minimum spanning tree problem
leetcode. 349. intersection of two arrays
Common skills of quantitative investment -- Drawing Part 1: Drawing stock closing price curve and ochl candle chart
论文笔记:STMARL: A Spatio-Temporal Multi-AgentReinforcement Learning Approach for Cooperative Traffic
MySQL performance analysis - explain
leetcode 142. Circular linked list II
Docker install MySQL
Characteristics of transactions -- atomicity (implementation principle)
Leetcode-17- letter combination of phone number (medium)
[Latex] 插入图片
Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing
Quick power explanation
Three paradigms of database
Matrix fast power
Jenkins continuous integration operation
关于#数据库#的问题,如何解决?