C++ copy function copy constructor destructor assignment operator

Constructor

1. Definition:

Each class defines the way its object is initialized. The class controls the process of its object initialization through one or several special member functions.
These functions are called constructors.

2. Features:

2.1

The name of the constructor is the same as the class name, and there is no return type; otherwise the parameter list of the constructor can be empty, and the function body can also be empty .

2.2

The constructor cannot be declared as const.

Three. The default constructor

#include<iostream>
using namespace std;
class Test
{
    
    
public:

private:
	int m_data;
};
void  main()
{
    
    
	Test t;//调用构造函数

Insert picture description here

We did not provide an initial value for the object t, so the default constructor initializes t.m_data to a random value


  • The default constructor is the constructor created by the compiler, also known as the synthetic default constructor
  • The default constructor has no arguments

Copy constructor

1. Definition:

If the first parameter of a constructor is a reference to its own class type, and any additional parameters have default values, we call this constructor: copy constructor

class Test
{
    
    
public:
	Test(int a =0)
	{
    
    
		m_data = a;
	}
	Test(const Test &t)
	{
    
    
		m_data = t.m_data;
	}
private:
	int m_data;
};
void  main()
{
    
    
	Test t;//调用构造函数
	Test t1 = t;//调用拷贝构造函数
}
  • The first parameter of the copy constructor must be a reference type. If the first parameter is not a reference type, then calling the copy constructor will never succeed, because we need to call the constructor for the formal parameters, which will cause an infinite loop phenomenon.

Destructor

1. Definition

The destructor releases the resources created by the constructor and destroys the non-static data members of the object.

2. Features

1. The destructor is a member function of the class
2. The name is composed of a tilde followed by the class name
3. There is no return value
4. It does not accept parameters

#include<iostream>
using namespace std;
class Test
{
    
    
public:

	~Test()//析构函数
	{
    
    

	}
private:
	int m_data;
};
void  main()
{
    
    
	Test t;//调用构造函数
}

-Since the destructor does not accept parameters, it cannot be overloaded. For a given class, there will be only one destructor

Overloaded assignment operator

1. Definition

An overloaded operator is essentially a function, and its name consists of the operator keyword followed by the symbol of the operator to be defined. The
assignment operator is a function named operator=

2. Features

Assignment operators should usually return a reference to the operand on the left

#include<iostream>
using namespace std;
class Test
{
    
    
public:

	Test& operator=(const Test &t)//传引用效率更高,不用调用构造函数为形参开辟空间
	{
    
    
		if (this != &t)//判断调用的形参是否为自己
		{
    
    
			m_data = t.m_data;
		}
		return *this;// 如果if里面条件成立直接返回,否则赋值之后返回
	}
private:
	int m_data;
};
void  main()
{
    
    
	Test t;//调用构造函数
	Test t1 = t;
}

The complete code for calling the four functions:

#include<iostream>
using namespace std;
class Test
{
    
    
public:
	Test(int a = 10)
	{
    
    
		m_data = a;
		cout << "Create Test Obj and the Address is: "<< this<<endl;
	}
	Test(const Test &t)
	{
    
    
		m_data = t.m_data;
		cout << "Create Test Obj and the Address is: " << this << endl;
	}
	Test& operator=(const Test &t)
	{
    
    
		if (this != &t)
		{
    
    
			m.data = t.m_data;
		}
		return *this;
	}
	~Test()
	{
    
    
		cout << "Free Test Obj and the Address is:" << this<<endl;
	}
private:
	int m_data;
};
void  main()
{
    
    
	Test t;//调用构造函数
	Test t1 = t;
}

Guess you like

Origin blog.csdn.net/ahuyccc/article/details/115034119