Classification and call types of constructors for c++ learning in-depth learning

text

Constructors are special functions used in C++ to create and initialize objects. Constructors can be classified according to different characteristics and parameters. The following are some common constructor classifications and explain their calling methods in detail:

Default constructor:

The default constructor is a special constructor that has no parameters and can have no specific implementation code. If no constructor is explicitly defined, the C++ compiler generates a default constructor for the class.
When the object is created, if no constructor parameters are provided, the compiler will automatically call the default constructor to initialize the object.
Example:

class MyClass {
public:
    // 默认构造函数(由编译器生成)
};

MyClass obj; // 调用默认构造函数

Parameterized constructor:

A parameterized constructor accepts one or more parameters, which are used to initialize member variables of an object.
Programmers must define parameterized constructors themselves, the compiler will not generate them automatically.
Example:

class MyClass {
public:
    // 参数化构造函数
    MyClass(int value) : someValue(value) {}
private:
    int someValue;
};

MyClass obj(42); // 调用参数化构造函数

Copy constructor:

A copy constructor is a special constructor used to create a copy of an object.
Copy constructors usually accept an object of the same type as a parameter in order to copy its state.
The copy constructor is called when an object is passed by value, returned by value, or copied by assignment.
Example:

class MyClass {
public:
    // 拷贝构造函数
    MyClass(const MyClass& other) : someValue(other.someValue) {}
private:
    int someValue;
};

MyClass obj1;
MyClass obj2 = obj1; // 调用拷贝构造函数

Delegating constructor (C++11 and later):

Delegating constructors allow one constructor to call another constructor of the same class to avoid code duplication.
Delegating constructors are invoked by using other constructors in the member initialization list.
Example:

class MyClass {
public:
    // 委托构造函数
    MyClass(int value) : someValue(value) {}
    MyClass() : MyClass(0) {} // 委托构造函数
private:
    int someValue;
};

Implicit call:

Constructors can be called automatically without being called directly, such as through object declaration and initialization, through assignment operations, passing as function parameters, and so on.
Example:

MyClass obj1;            // 调用默认构造函数
MyClass obj2 = obj1;     // 调用拷贝构造函数
MyClass obj3(obj1);      // 调用拷贝构造函数

These different types of constructors allow programmers to perform different initialization operations when creating objects, and choose the appropriate constructor according to their needs. With the continuous development of the C++ standard, the function and usage of the constructor are also evolving, making the creation and initialization of objects more flexible and powerful.

Notice

  • Do not add () when calling the no-argument constructor, otherwise the compiler thinks it is a function declaration
  • Anonymous objects will be recycled if they are not used
  • Do not use the copy constructor to initialize anonymous objects, that is, if person ( p3 ) is equivalent to person p3; similar to redefinition

Guess you like

Origin blog.csdn.net/wniuniu_/article/details/132635618