C++. Keywords default, delete. Examples of using




Keywords default, delete. Examples of using


Contents


Search other websites:

1. Using the default keyword. General form

The default keyword is introduced in C ++ 11. Its use tells the compiler to independently generate the corresponding class function, if one is not declared in the class.

As you know, the compiler automatically generates a number of class constructors and a destructor. Specifying the default keyword for these functions provides the following interrelated benefits:

  • the programmer receives understandable information about the lack of implementation of certain functions in the class. Compiler versions of these functions are used;
  • the perception of information about the peculiarities of using certain functions of the class is simplified. Instead of remembering the rules, the programmer specifies what he wants to get.

The general form of using the default keyword for class constructors is as follows:

class ClassName
{
  // ...

  ClassName(parameters) = default;

  // ...
};

here

  • ClassName – name of the class in which the default keyword is used;
  • ClassName(parameters) – one of the class constructors that are automatically generated by the compiler;
  • parameters – a list of parameters that a special function of the class takes that is automatically generated. The parameter list may be missing (default constructor).

General form of using the default keyword for a class destructor

class ClassName
{
  // ...

  ~ClassName() = default;

  // ...
};

 

2. What special class functions can the default keyword apply to?

The default keyword can only be applied to special class functions that are automatically generated by the compiler when the class is declared. These functions are:

  • default constructor;
  • copy constructor;
  • move constructor;
  • copy assignment operator;
  • move assignment operator;
  • destructor.





 

3. An example of using the default keyword for special class functions that are automatically generated by the compiler

The following demonstrates the use of the default keyword for all special class functions that are automatically generated.

A Float class is declared that implements a real number.

In the class are declared:

  • internal hidden (private) variable x;
  • default constructor;
  • default copy constructor;
  • default move constructor;
  • default copy assignment operator;
  • default move assignment operator;
  • default destructor;
  • access methods Get(), Set().

 

// class Float, demonstrating the default keyword
class Float
{
private:
  float x;

public:
  // default constructor
  // The default keyword tells the compiler to independently form
  // the appropriate constructor, if one is not declared in the class
  Float() = default;

  // constructor with 1 parameter - default cannot be applied
  Float(float _x) :x(_x)
  { }

  // copy constructor - transferring rights to the compiler to form this constructor
  Float(const Float&) = default;

  // move constructor
  Float(Float&&) = default;

  // copy assignment operator
  Float& operator=(const Float&) = default;

  // move assignment operator
  Float& operator=(Float&&) = default;

  // destructor
  ~Float() = default;

  // access methods
  float Get() { return x; }
  void Set(float _x) { x = _x; }
};

void main()
{
  // default - constructor
  Float f1(7);
  Float f2 = f1;

  cout << "f2.x = " << f2.Get() << endl;

  Float f3;
  f3.Set(18);
  f3 = f2;
  cout << "f3.x = " << f3.Get() << endl;
}

The result of the program

f2.x = 7
f3.x = 7

 

4. The purpose of the delete keyword. The general form for declaring a constructor and method of a class with the delete keyword

The delete keyword is used when it is necessary to disable automatic type conversion in class constructors and methods.

The general form of the class constructor declaration using the keyword delete

class ClassName
{
  // ...

  // constructors that are disabled to use
  ClassName(parameters1) = delete;
  ClassName(parameters2) = delete;
  
  // ...

  ClassName(parametersN) = delete;

  // ...
};

here

  • parametes1, parameters2, …, parametersN – list of parameters of the corresponding class constructor.

Parameters may be missing. If there are no constructor parameters, it is disabled to use the default constructor. In this case, when declaring

ClassName obj1;

the compiler will generate the error

The default constructor of "ClassName" cannot be referenced – it is a deleted function

The delete keyword can also be disabled in methods. In this case, for a method called Func(), the general declaration form will be as follows

class ClassName
{
  // ...

  return_type Func(parameters) = delete;

  // ...
};

here

  • Func() – the name of the function to be disabled from using in the class;
  • parameters – function parameters;
  • return_type – type returned by function.

 

5. An example of using the delete keyword in a class

The Point class is implemented, which implements a point on the plane. The following elements are declared in the class:

  • private variables with names x, y – point coordinates;
  • default constructor Point(), indicated by the keyword default. The default keyword indicates that the implicit default constructor should be executed, which is automatically generated by the compiler;
  • constructor Point(int) with one parameter of type int;
  • constructor with two parameters Point(int, int);
  • access methods GetX(), GetY(), SetX(), SetY();
  • operator function operator+() overloading the binary operator +;

Class disables the use a constructor with one parameter Point(double), which takes a parameter of type double. This constructor is indicated by the delete keyword. However, a constructor with an int parameter is allowed to be used in the class.

It is also forbidden to use some functions:

  • function SetX(double) with a parameter of type double. However, Set(int) with an argument of type int is allowed to be used;
  • function SetXY(int, int) with two parameters of type int.

The text of the Point class is as follows:

#include <iostream>
using namespace std;

// A class that implements a point on the coordinate plane
// the class examines the keywords default and delete
class Point
{
private:
  int x = 0; // coordinates of a point
  int y = 0;

public:
  // default constructor, trust the implementation to the compiler
  Point() = default;

  // constructor with one parameter of type double - it is disabled to use
  Point(double value) = delete;

  // constructor with one parameter of type int - allowed to use
  Point(int value)
  {
    x = y = value;
  }

  // constructor with 2 parameters
  Point(int nx, int ny)
  {
    x = nx;
    y = ny;
  }

  // class member access methods
  int GetX(void) { return x; }
  int GetY(void) { return y; }

  // functions SetX(), SetY() with parameter of type int
  void SetX(int nx) { x = nx; }
  void SetY(int ny) { y = ny; }

  // disable the calling the SetX(), SetY() functions with a parameter of type double
  void SetX(double) = delete;

  // information for the compiler that it is disabled to use
  // the SetXY() method with two int parameters
  void SetXY(int, int) = delete;

  // the SetXY() method itself is unnecessary, the compiler throws an error
  /*
  void SetXY(int nx, int ny)
  {
    x = nx;
    y = ny;
  }
  */

  // overloaded binary operator '+'
  Point operator+(Point& pt)
  {
    // p - temporary object that is created using the constructor without parameters
    Point p;
    p.x = x + pt.x;
    p.y = y + pt.y;
    return p;
  }

  // overloaded unary operator '-'
  Point operator-(void)
  {
    Point p;
    p.x = -x;
    p.y = -y;
    return p;
  }

  void Show(const char* objName)
  {
    cout << objName << ":" << endl;
    cout << "x=" << x << "; y=" << y << endl;
    cout << endl;
  }
};

void main()
{
  // 1. Declaring an object using a constructor with 1 parameter
  // 1.1. Compiler error
  //Point p1(5.5); // constructor with 1 parameter of type double is forbidden

  // 1.2. It is allowed to use a constructor with 1 parameter of type int
  Point p1(5);
  p1.Show("p1");

  // 1.3. It is disabled to use the SetX () method with a parameter of type double
  // p1.SetX(5.5);
  p1.SetY(3.8); // but the SetY() method can be used
  p1.Show("p1");

  // 2. Declaring an object without parameters - the default constructor is called
  Point p3; // the default constructor of comiler is called
  p3.Show("p3");

  // Declaring an object using a constructor with 2 parameters
  Point p4(7, 8);
  p4.Show("p4");
}

Explain some code fragments. If the function main() specify

Point p1(5.5);

then the compiler will throw an error

(double)(5.5)

Function "Point::Point(double value)" declared at line (18) cannot be referenced - it is a deleted function

This means that it is forbidden to use a constructor with 1 parameter of type double. However, it is allowed to use a constructor with 1 parameter of type int, since this constructor is specified without the delete keyword.

In the Point class, the delete keyword also applies to the function

void SetX(double) = delete;

This means that it is forbidden to call the SetX() function with a parameter of type double. However, the class contains another SetX() function with an int parameter

void SetX(int nx) { x = nx; }

which is allowed to call. Thus, in the SetX() function, automatic cast from double to int is prohibited. Using a similar example, you can disable automatic type conversion for any class functions.

For demonstration purposes, it is shown that in the Point class it is forbidden to use the SetXY() function with two parameters

void SetXY(int, int) = delete;

Although, there is no other implementation of this function in the class. Such prohibitions of not implemented functions in a class are normally perceived by the compiler.

 


Related topics