For an empty class, the compiler produces 4 member functions by default: The default constructor, destructor, copy constructor, and assignment function.
1. Constructor:
A constructor is a special class member that is called to initialize and allocate memory for a class's data members when a class is created. Constructors must be named exactly the same as the class name, constructors can be overloaded, can be multiple, and can take parameters.
eg
Class A
{
Public
A () {cout<< "no parameter constructor";}
A (int i) {cout<< "with parameter constructor";}
};
A ();//Call the default constructor
A (1);//Call a parameter constructor
Creating an object calls the parameterless constructor, which is the default constructor, and the constructor of the argument needs to be called by itself.
2. destructor
Constructors can be overloaded, destructors cannot be overloaded, destructors are always only one, and if the destructor is not written, C + + will automatically write a destructor for us.
Constructors and destructors are a pair, constructors are used to create objects, and destructors are used to undo objects.
eg
#include <iostream>
using namespace std;
class A
{
public:
A ()
{
cout << "The constructor is called!" << endl;
}
~ A () {cout << "The destructor was called" << endl;}
};
int main ()
{
A a; // Call the constructor and destructor
A * p = new A; // Call the constructor
delete p; // Call the destructor
return 0;
}
3. Copy constructor function
C + + Copy constructors (deep copy, shallow copy)
4. Assignment function
This function is used when an object of a class assigns a value to another object of the class.
When there is no overloaded assignment function, the assignment is performed by the default assignment function.
eg
#include <iostream>
using namespace std;
class A
{
public:
A () {cout << "call constructor" << endl;}
A (char s) {cout << s << "call constructor" << endl;}
~ A () {cout << "Call destructor" << endl;}
};
int main ()
{
A a (‘a‘);
A b (‘b‘);
b = a; // a, b actually exists, then call the assignment function, assign a to b
A c (a); // Copy constructor
return 0;
}
The difference between a copy function and an assignment function:
1. The copy constructor is an object that initializes a chunk of memory, which is the memory area of the new object, and the assignment function is an assignment to an object that has already been initialized.
2. In general, when a data member contains a pointer object, two different processing requirements need to be considered: one is to copy the pointer object, and the other is to reference the pointer object. Copy constructors are most often copied, whereas assignment functions are reference functions.
3, the realization is different. The copy constructor is first a constructor, which is called when an object is generated by initializing an object with a parameter. The assignment function is to assign a new object to an original object, so if there is memory allocation in the original object to release the memory first, but also to check whether two objects are not the same object, if so, do nothing, directly return.
#include <iostream>
using namespace std;
class A
{
public:
A () {cout << "call constructor" << endl;}
A (char s) {cout << s << "call constructor" << endl;}
A (const A & other);
A & operator = (const A & a); // Overload copy function
~ A () {cout << "Call destructor" << endl;}
};
A & A :: operator = (const A & a)
{
A example;
cout << "Call copy function" << endl;
return example;
}
int main ()
{
A a (‘a‘);
A b (‘b‘);
b = a; // a, b actually exists, then call the assignment function, assign a to b
return 0;
}
Summarize:
object does not exist and is initialized with another object, the constructor is called
object does not exist and is initialized with another object, that is, the copy constructor
object exists, assigns it to another object, which is the assignment function
The program ends, and each object calls the destructor
Reference Address: 52132936
In C + +: Default constructors, destructors, copy constructors, and assignment functions--Go