1

I am trying to understand the behaviour of auto-generated compiler code for various functions such as:

  1. destructor
  2. copy constructor
  3. assignment operator
  4. move constructor
  5. move assignment operator

Will declaring them with "= default" cause any functional difference compared to non-declared case? Does the answer of this question differ among the above listed functions? If there is no functional difference, what are the consequences of using either case?

Copy constructor declared with "= default"

class MyClass
{
public:
    MyClass();
    MyClass(MyClass &other) = default;
    
    OtherClass some_member;
};

Copy constructor not declared:

class MyClass
{
public:
    MyClass();

    OtherClass some_member;
};
Yiğit
  • 55
  • 6

1 Answers1

0

In some cases the copy constructor is deleted by default. The most simplest example:

class myClass {

public:
    myClass();
    myClass(myClass &&);
};

myClass a;

void func()
{
    myClass b=a;  // ERROR
}

Rather than explaining the reason for the compilation error, I'll just paste the compilation error, verbatim, from my compiler:

‘constexpr myClass::myClass(const myClass&)’ is implicitly declared as deleted because ‘myClass’ declares a move constructor or move assignment operator

Explicitly declaring a default or a user-defined copy constructor will make the code compile.

There are also several other reasons. An explicitly declared copy constructor deletes the implicitly declared move constructor, for one.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • thank you for your answer sam, will defining any of the 5 above functions cause the exact same compilation error? – Yiğit Oct 24 '20 at 16:45
  • Many of these rules vary, depending on the class methods. – Sam Varshavchik Oct 24 '20 at 16:56
  • what do you mean by class methods? any member method or one of the 5? how can I learn those rules? – Yiğit Oct 24 '20 at 16:58
  • 2
    All, or most of these rules should be explained in every advanced C++ textbook covering C++11 or later. Stackoverflow.com is not really a replacement for a textbook, it wouldn't make much sense to copy/paste the entire chapter that explains all of them, would it? What explanation do you find in your textbook, and is there anything specific about the explanation that's unclear to you? – Sam Varshavchik Oct 24 '20 at 17:01
  • i do not currently have a textbook, I would like to read one if you recommend me a good book. however, reading a textbook from cover to cover is rather an inefficient way of learning one specific thing but a good way of mastering the subject as a whole. so I just want to understand the differences between: empty definitions ( with { } ), defining as "= default" in the implementation file, declaring as "= delete", declaring as "= default" or not declaring at all. – Yiğit Oct 24 '20 at 17:07
  • 1
    See [stackoverflow's list of C++ textbooks](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). It is true that a textbookiis not for "learning one specific thing but a good way of mastering the subject". Since presumably this is your long term goal (doesn't make sense to me to stop learning C++ after learning only this topic), so you need a textbook. C++ is the most complicated and hardest general purpose programming language in use today. Nobody will learn C++ from a random Youtube video or an online coding site, but only from a textbook. – Sam Varshavchik Oct 24 '20 at 17:10