Tuesday, 29 January 2019

C++ Deleted and default

default
If a class is defined with any constructors, the compiler will not generate a default constructor. This is useful in many cases, but it is some times vexing. For example, we defined the class as below:
class A
{
public:
    A(int a){};
};
Then, if we do:
A a;
The compiler complains that we have no default constructor. That's because compiler did not make the one for us because we've already had one that we defined.
We can force the compiler make the one for us by using default specifier:
class A
{
public:
    A(int a){}
    A() = default;
};
Then, compiler won't complain for this any more:
A a;


delete
Suppose we have a class with a constructor taking an integer:
class A
{
public:
    A(int a){};
};
Then, the following three operations will be successful:
A a(10);     // OK
A b(3.14);   // OK  3.14 will be converted to 3
a = b;       // OK  We have a compiler generated assignment operator
However, what if that was not we wanted. We do not want the constructor allow double type parameter nor the assignment to work.
C++11 allows us to disable certain features by using delete:
class A
{
public:
    A(int a){};
    A(double) = delete;         // conversion disabled
    A& operator=(const A&) = delete;  // assignment operator disabled
};
Then, if we write the code as below:
A a(10);     // OK
A b(3.14);   // Error: conversion from double to int disabled
a = b;       // Error: assignment operator disabled
In that way, we could achieve what we intended.

No comments:

Post a Comment