C++ default Keyword

The default keyword in C++ is used to explicitly instruct the compiler to generate a default implementation for special member functions such as constructors, destructors, and assignment operators.


Syntax

</>
Copy
class ClassName {
public:
    ClassName() = default;                 // Default constructor
    ClassName(const ClassName&) = default; // Default copy constructor
    ClassName& operator=(const ClassName&) = default; // Default copy assignment operator
    ~ClassName() = default;                // Default destructor
};
= default
Specifies that the compiler should generate the default implementation for the member function.
ClassName
The name of the class where the default keyword is applied.

Examples

Example 1: Using default for Special Member Functions

This example demonstrates the use of the default keyword to generate default implementations for special member functions.

</>
Copy
#include <iostream>
using namespace std;

class MyClass {
public:
    int value;

    // Default constructor
    MyClass() = default;

    // Default copy constructor
    MyClass(const MyClass&) = default;

    // Default copy assignment operator
    MyClass& operator=(const MyClass&) = default;

    // Default destructor
    ~MyClass() = default;
};

int main() {
    MyClass obj1;          // Calls default constructor
    obj1.value = 10;

    MyClass obj2 = obj1;   // Calls default copy constructor
    cout << "Value in obj2: " << obj2.value << endl;

    return 0;
}

Output:

Value in obj2: 10

Explanation:

  1. The class MyClass uses = default for its default constructor, copy constructor, copy assignment operator, and destructor.
  2. The default constructor initializes the obj1 object.
  3. The default copy constructor is invoked to create obj2 as a copy of obj1.
  4. The value of obj2.value is printed, showing that the default implementations work as expected.

Example 2: Preventing Custom Implementations

This example demonstrates how the default keyword can prevent developers from accidentally writing unnecessary custom implementations for member functions.

</>
Copy
#include <iostream>
using namespace std;

class NoCustomConstructor {
public:
    int value;

    // Explicitly use default implementations
    NoCustomConstructor() = default;
    NoCustomConstructor(const NoCustomConstructor&) = default;
    ~NoCustomConstructor() = default;

    void display() const {
        cout << "Value: " << value << endl;
    }
};

int main() {
    NoCustomConstructor obj1; // Default constructor
    obj1.value = 42;

    NoCustomConstructor obj2 = obj1; // Default copy constructor
    obj2.display();

    return 0;
}

Output:

Value: 42

Explanation:

  1. The class NoCustomConstructor uses = default to specify that the compiler should generate default implementations for the constructor, copy constructor, and destructor.
  2. The display method prints the value of the value attribute.
  3. This ensures the default behavior is used, and no unnecessary custom implementations are written.

Key Points about default Keyword

  1. The default keyword instructs the compiler to generate a default implementation for a special member function.
  2. It can be used with constructors, destructors, and assignment operators.
  3. Using = default prevents unnecessary custom implementations, ensuring consistency and maintainability.
  4. Introduced in C++11, it simplifies code and reduces boilerplate when default behavior is sufficient.