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:
- The class
MyClassuses= defaultfor its default constructor, copy constructor, copy assignment operator, and destructor. - The default constructor initializes the
obj1object. - The default copy constructor is invoked to create
obj2as a copy ofobj1. - The value of
obj2.valueis 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:
- The class
NoCustomConstructoruses= defaultto specify that the compiler should generate default implementations for the constructor, copy constructor, and destructor. - The
displaymethod prints the value of thevalueattribute. - This ensures the default behavior is used, and no unnecessary custom implementations are written.
Key Points about default Keyword
- The
defaultkeyword instructs the compiler to generate a default implementation for a special member function. - It can be used with constructors, destructors, and assignment operators.
- Using
= defaultprevents unnecessary custom implementations, ensuring consistency and maintainability. - Introduced in C++11, it simplifies code and reduces boilerplate when default behavior is sufficient.
