In C++, a constructor is a special member function that is used to initialize class objects. It is automatically called when the object is created. The main function of the constructor is to allocate resources, initialize data members, etc. Depending on different functions and usage scenarios, C++ provides multiple types of constructors:
1. Default constructor
The default constructor does not accept any parameters, or all parameters have default values. When you create an object without specifying parameters, the default constructor is called.
Features:
- If the class does not define any constructor, the compiler will automatically generate a default constructor.
- If the class has other constructors but does not explicitly define the default constructor, the compiler will not generate the default constructor.
Example:
class MyClass { public: MyClass() { // Default constructor x = 0; } private: int x; }; MyClass obj; // Automatically call the default constructor
2. Parameterized constructor
Parameterized constructors allow parameters to be passed when creating an object, which is used to initialize member variables of the object.
Features:
- By passing parameters, you can flexibly assign values to objects.
Example:
class MyClass { public: MyClass(int val) { // Parameterized constructor x = val; } private: int x; }; MyClass obj(10); // Use parameterized constructors
3. Copy constructor
When a copy constructor is used to create an object, it initializes a new object with an existing object. Its form is to accept a constant reference to an object.
Features:
- If not explicitly defined, the compiler will automatically generate a default copy constructor.
- Mainly used to copy the values of objects, especially for classes that dynamically allocate memory, manually defining the copy constructor can prevent shallow copy problems.
Example:
class MyClass { public: MyClass(int val) : x(val) {} // Parameterized constructor MyClass(const MyClass &obj) { // Copy constructor x = ; } private: int x; }; MyClass obj1(10); MyClass obj2 = obj1; // Use copy constructor
4. Move constructor
The mobile constructor was introduced in C++11 to avoid copy operations through "mobile semantics", thereby improving program performance, especially objects involving dynamic allocation of resources.
Features:
- Accept an rvalue reference (
T&&
)。 - Used to "move" resources from a temporary object into a new object, usually by stealing resources rather than copying them.
Example:
class MyClass { public: MyClass(int val) : x(new int(val)) {} // Dynamically allocate memory MyClass(MyClass&& obj) noexcept { // Move constructor x = ; = nullptr; // Release ownership of temporary objects } ~MyClass() { delete x; } private: int* x; }; MyClass obj1(10); MyClass obj2 = std::move(obj1); // Using the move constructor
5. Delegating constructor
A delegate constructor is to call another constructor of the same class in one constructor, thereby avoiding code duplication. This is a feature introduced in C++11.
Features:
- The code logic between multiple constructors can be simplified and the code can be avoided.
Example:
class MyClass { public: MyClass() : MyClass(0) { // Delegate to parameterized constructor // Some additional operations can be performed } MyClass(int val) : x(val) {} // Parameterized constructorprivate: int x; }; MyClass obj; // Call the default constructor,Delegate to parameterized constructors
6. Explicit constructor
explicit
The constructor is used to prevent implicit type conversion. This is especially useful for preventing errors in automatic type conversion.
Features:
- Prevents constructors from being automatically called for implicit conversions.
Example:
class MyClass { public: explicit MyClass(int val) : x(val) {} // Explicit constructorprivate: int x; }; MyClass obj1(10); // OK MyClass obj2 = 10; // mistake,Explicit constructors prohibit implicit conversions
7. Destructor
Although the destructor is not a constructor, it has a similar effect. The destructor is used to free resources at the end of the object's life cycle. The destructor has no parameters and has~
Symbol.
Example:
class MyClass { public: MyClass() { x = new int(10); } ~MyClass() { delete x; } // Destructorprivate: int* x; };
Summarize:
The types and uses of constructors in C++ can be summarized as follows:
- Default constructor: Initialize the object, usually no parameters are required.
- Parameterized constructor: Initializes the member of the object by passing the parameter.
- Copy constructor: Initialize a new object with an existing object.
- Move constructor: Move the resource of the object to avoid unnecessary copying.
- Delegate constructor: Call another constructor in one constructor.
- Explicit constructor: Prevent implicit type conversion.
These constructors provide flexible options for initialization of objects, especially when managing resources, the rational use of copy and move constructors can significantly improve the efficiency of the program.
This is all about this article about 6 constructors in C++. For more related contents of C++ constructors, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!