-2
class MyClass : public ParentClass  
{  
public:  
    explicit MyClass(classA a, const classB b) : A(a), B{b} {}  
 
    MyClass() override = default;  
    MyClass(const MyClass&) = delete;  
    MyClass(MyClass&&) = delete;  
    MyClass& operator=(const MyClass&) = delete;  
    MyClass& operator=(MyClass&&) = delete;  

    void parentFunc() override;  
    void childFunc();  

private:  
    classA& A;  
    const classB& B;  
};  

What is the use of default and delete modifiers?
How it actually impacts the logic?

Eugene Mihaylin
  • 1,736
  • 3
  • 16
  • 31
Naresh
  • 11
  • delete means that the compiler will not generate those constructors when declared – Eugene Mihaylin Apr 20 '23 at 18:32
  • The delete keyword is used when it is necessary to disable automatic type conversion in class constructors and methods. – Eugene Mihaylin Apr 20 '23 at 18:33
  • If you're wondering, I had two reasons why I downvoted: 1) you're asking others to invest time helping you, but didn't bother to fix the formatting in your question to make it easier for them 2) you're asking for an explanation of a fairly basic language feature; there are hundreds of books and tutorials out there covering this. If you're having trouble understanding specific parts of the feature, then SO can help you. – Wutz Apr 20 '23 at 21:13

1 Answers1

0

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.

From here: https://www.bogotobogo.com/cplusplus/C11/C11_default_delete_specifier.php

Eugene Mihaylin
  • 1,736
  • 3
  • 16
  • 31