...

/

Request and Suppress Examples

Request and Suppress Examples

Let's see examples of how the `default` and `delete` keywords can be used to our benefit.

Constructors and destructors using default

Let’s take a look at the code in the following example:

C++
#include <iostream>
class SomeType{
public:
// state the compiler generated default constructor
SomeType() = default;
// constructor for int
SomeType(int value){
std::cout << "SomeType(int) " << std::endl;
};
// explicit Copy Constructor
explicit SomeType(const SomeType&) = default;
// virtual destructor
virtual ~SomeType() = default;
};
int main(){
std::cout << std::endl;
SomeType someType;
SomeType someType2(2);
SomeType someType3(someType2);
std::cout << std::endl;
}

Explanation

In this example, we can see how default can be used to get the default implementations of constructors and destructors from the compiler.

  • Since we have defined a parameterized constructor in line 10, the compiler will not automatically create a default constructor.

  • Hence, we have to ...