C++ study notes ten-class copy constructor and destructor

The last chapter introduced the constructor of the class. We can know that the function of the constructor is to initialize the members of the class, so what is the copy constructor used for? The copy constructor can also be simply understood as a function for initialization. It is mainly used in the following three situations:
1. In an already initialized class, if you want to use this class to initialize another class, simply understand it as a new class Assignment.
2. When the function is called, if the formal parameter of the function is an object of type, the copy constructor will be called when the actual parameter is converted to each other.
3. When the object of the class is used as the return value, the copy constructor will be called upon return.
The basic form of the copy constructor

类名 (const  类名 &p);//复制构造函数的形式,这里其实去掉const 依然可以,但是为了数据安全不修改实参的值,通常改为如此,可以简单的理解为只读的模式

The following is a simple example to illustrate the three cases of calling the copy constructor

# include<iostream>
# include <string>
using namespace std;
//定义point类,私有成员数据为 x y
class point {
    
    
public:
 
	point(int xx=0, int yy=0) :x(xx), y(yy){
    
    }//构造函数。初始化列表构造方式
	point(const point &p);//复制构造函数
	int getx() {
    
     return x; }//获取成员x的值
	int gety() {
    
     return y; }//获取成员y的值


private:
	int x, y;

};
//构造函数的定义方式,数值一一复制,也叫浅拷贝
point::point(const point &p)
{
    
    
	x = p.x;
	y = p.y;
	cout << "calling copy constructor" << endl;
}
//将函数的形参定义为类的对象。输出其中的值
void fun1(point p1)
{
    
    
	cout << "the x value is" << p1.getx() << endl;
}
//类的对象作为返回值类型,简单进行反转
point fun2(point p1,point p2)
{
    
    
	point p3;
	p3 = p1;
	p1 = p2;
	p2 = p3;
	return p1;
}
int main()
{
    
    
void fun1();
point fun2(point p1,point p2);
point a;
point b(a);//使用当前已有对象去初始化另一个对象,会使用构造函数
cout << "b.x " << b.getx() << endl;
cout << "b.y " << b.gety() << endl;
point c(1, 2);//初始化c
fun1(c);//输出第一个的值
point c1 = fun2(c, a);//反转
cout << "the swap c1 x is" << c1.getx() << endl;

return 0;



}

The results of the program are as follows
Insert picture description here

After reading the above copy constructor, you may have a question in your mind. This one-to-one corresponding value copy does not seem to be worthwhile for us to write a special function. It seems that the copy constructor is useless, but in fact, this is just to facilitate our understanding and use. Because of the shallow copy, I think the copy constructor is a bit cumbersome. When we use deep copy, we will continue to introduce the copy constructor in detail.

So what is a destructor? Let's take a function as an example. If data is defined in the function, then its life cycle only exists in the function body. Once the function runs, the data memory will also die. Then for the object of the class, after initialization How to release memory? Simply put, it is to use the destructor to release the memory . If we do not define a destructor, the system will generate a default destructor. Taking the above class as an example, the form of the destructor is:

~point()//析构函数同样不能有返回值

Guess you like

Origin blog.csdn.net/qq_41803340/article/details/112548463