C + + class constructors, destructors, copy constructors, and problems with implicitly instantiating objects

Keywords: C++

C + + constructors and destructors are used for object initialization and cleaning. When each class is defined, three functions will be created by default - constructors, destructors and copy constructors

1. Default constructor

Constructors are similar to python's__ init__ Method, which is called automatically and only once when the class is instantiated with an object. The default constructor has no parameters and the content is empty. You can modify the default constructor by rewriting the class name (parameter) {},    Constructors, like ordinary functions, can be overloaded, so there can be multiple construction parameters in a class. During instance initialization, the call will be automatically identified according to the parameters passed.

#include <iostream>
using namespace std;
#include <string>

class PersonTest {

    string name;

public:  //Note that the destructor must be a public method construct, depending on the situation
    // Constructors are similar to init
    // Multiple functions are similar to overloads and automatically recognize calls
    PersonTest() {
        cout << "Call to default parameterless constructor" << endl;

    PersonTest(string name1) {
        cout << "Call with parameter constructor" << endl;
        name = name1;
        cout << name << endl;


int main() {

    PersonTest p1;
    string name = "name1";
    PersonTest p2(name);
    PersonTest p3("name2");

    return 0;

Instantiate the object in the main function (using the bracket method), and the input results are as follows:

  When initializing the instance object, the corresponding constructor will be called according to the parameter type you pass in;

2. Copy constructor

The parameter of the copy constructor is the reference class name (const class name & object) {} of an instantiated object. The function is used to copy the incoming instance object to another object with the same properties. The default copy constructor is to completely copy all the attributes passed into the instance. After rewriting the copy constructor, you can copy the specified attributes.

    //The copy constructor will be called automatically when the function value of the copy constructor is passed. The function return object is also the value created by calling the copy function to pass the value return
    PersonTest(const PersonTest & p) {
        cout << "Copy constructor call" << endl;
        name = p.name;
        cout << name << endl;


When instantiating an object, a created object is passed in, and the copy constructor will automatically call:


  The output result is:


  The copy constructor is called in three cases:   1 copy and create a new instance by using the existing instance, 2 take the instance as the formal parameter of the function, and 3 take the instance as the return value of the function

3. Destructor

Destructors are similar to those in python__ del__ Function, called when the instance object is destroyed; The default destructor parameter is null, and the content is null. The destructor rewriting method is ~ class name () {}; Note that the destructor cannot be overloaded;


    // Destructors are similar to del Method cannot be overloaded
    ~PersonTest() {
        cout << name << endl;
        cout << "Destructor call" << endl;


The destructor must be a public method, otherwise the system call will make an error when the instance is destroyed;





  4. Several methods of creating objects

1. Bracket method    2. Display method    3. Implicit transformation method

Code directly here

int main() {

    PersonTest p1;
    string name = "name1";
    PersonTest p2(name);
    PersonTest p3("name2");
    // copy constructor 
    PersonTest p03(p3);

    //Display method
    PersonTest p4; //The three methods are the same without parameters
    PersonTest p5 = PersonTest(name);
    PersonTest p6 = PersonTest("name2");
    PersonTest p7 = PersonTest(p6);

    //Implicit transformation method
    PersonTest p8;
    PersonTest P9 = name;
    // PersonTest P9 = "name"; It's an error because C++The default string of undefined strings is char[]type      use cout << typeid("name").name() << endl;View variable types
     PersonTest P10 = P9;

    return 0;

The following points need to be noted:

1 during implicit conversion, the constructor parameter type is string, so it cannot be used   PersonTest P9 = "name"; An error will be reported because C + + defaults to a string of undefined strings of char [] type, using cout < < typeID ("name"). Name() < < endl; View variable types; You can first define with string name = "name", and then pass parameters with name;

2 when writing in display method    PersonTest(name); In fact, it is an anonymous object. If you do not give an lvalue to an anonymous object, an instance object will be created when the statement is executed, and the object will be destroyed after the execution of the line; In particular, do not use the copy constructor to initialize anonymous objects; For example, PersonTest(p6) will report an error and redefine; The compiler will consider it a declaration of the object p6;


Posted by 7pm on Mon, 08 Nov 2021 14:45:34 -0800