C ++ constructor and initialization list

I'm not the creator of notes, I'm just a porter of notes ~

1. The constructor can be overloaded, or it can be a
    parameterless constructor
    string s that
    matches string with default parameters           ; a parameter (const char *) constructor
    string s ("hello") that matches string      

2, the default constructor (no parameter constructor)
   1) If no constructor is defined in the class, the compiler will provide a default (no parameter) constructor for the class:
        -"Do not do for basic type member variables Initialization-
        "For member variables of class type (member sub-objects), it will automatically call the parameterless constructor of the corresponding class to initialize

   2) If you define a constructor yourself, regardless of whether there are parameters, then the compiler will no longer provide the default parameterless constructor.

3, type conversion constructor (single-parameter constructor) // type conversion of the constructor   
    class class name {
        // Can convert source type variables to current class type objects.
        Class name (source type) {...}
    };
    -----------------------------------
    class class name {
        // Add "explicit" keyword modification, you can Mandatory that this type
        // conversion must be done
        explicitly . Explicit class name (source type) {...}
    };

4. The copy constructor (copy constructor)  I think this part is quite difficult to understand. 
1) Use an existing object as the construction parameter of similar objects. When creating a new copy object, the copy constructor will be called.
    class class name {
        class name (const class name &) {// copy structure
            ...
        }
    };
    ------------
    eg:
    class A {...};
    A a1 (.. .);
    A a2 (a1); // Matching A's copy constructor


2) If a class does not define its own copy constructor, then the compiler will provide a default copy constructor for the class:
-> For member variables of the basic type, copy by byte
->> For members of the class type Variable (member sub-object), will automatically call the copy constructor of the corresponding class to initialize

 

Note: It is generally not necessary to define the copy constructor function by yourself, because the compiler provides it by default is already very easy to use.

    class A1 {}; // No parameter by default, default copy
    class A2 {// default copy
        A (void) {}
    };
    class A3 {// default copy
        A (int) {}
    };
    class A4 { // There is no default structure
        A (const A &) {}
    };

3) The timing of copy constructor call-
"Use an existing object as the construction parameter of the same kind of object-
" Pass parameters to the function in the form of object-
"Return the object from the function (it may be optimized by the compiler)

Published 9 original articles · Likes6 · Visits 1995

Guess you like

Origin blog.csdn.net/GG802312/article/details/105593406