3

Let us assume we have a class X and we want wo explicitly forbid, let say the standard constructor. I used for a long time in the Header file:

private:
    X(); // 1.

so, that the contructor was disabled outside the class, so for anybody. But recently I have learned that in C++11 follwoing was recommended:

X() = delete; // 2.

Both would achive my wish to forbid the standard contructor.

But what is the exact difference bewteen them? Why would C++11 recommend the last one? Are there any other flags, signals set in the 2. way?

Ralf Wickum
  • 2,850
  • 9
  • 55
  • 103
  • In this particular case, neither. There's never a reason to disable the default constructor. – avakar Jul 23 '15 at 14:07
  • 2
    With the first you can still call the default from within the class, which is not possible with the second version. – Borgleader Jul 23 '15 at 14:07
  • 5
    @avakar: There _are_ reasons for disabling the default constructor. – Paul J. Lucas Jul 23 '15 at 14:10
  • @PaulJ.Lucas, name one. – avakar Jul 23 '15 at 14:10
  • @PaulJ.Lucas, no please, this time seriously, name one. Given the upvotes on your comment, I'm starting to think that maybe I'm the one who's out of their mind. – avakar Jul 23 '15 at 14:15
  • 1
    @avakar: Whenever you are looking at an object class for which no sensible "default" exists, i.e. that would *always* need data to be initialized *with*. Deleting the default constructor ensures that no non-sensible, non-initialized object gets created by accident. – DevSolar Jul 23 '15 at 14:16
  • @DevSolar, I don't understand. Either the class has other constructors and then there's no reason to delete it's default one (as it doesn't get generated) or it doesn't and then the class can't be instantiated (and then you should use namespace). – avakar Jul 23 '15 at 14:20
  • @DevSolar, oh, I understand what you're saying now -- in that case, again, the class will have other constructors, therefore no need to delete the default one. – avakar Jul 23 '15 at 14:21
  • @avakar: It is about clarity of intent. Explicitly `= delete`'ing the default constructor adds information that *you do not want it to be created*. (Imagine that intern looking at that class and going, "this doesn't have a default constructor, let's add one"...) The same as `// EMPTY` comments in constructors with initializer lists but empty body. "No, I have not forgotten something, this is *intentional*." – DevSolar Jul 23 '15 at 14:25
  • @DevSolar, man, I wish you were in chat, I can't believe my eyes :) – avakar Jul 23 '15 at 14:30
  • @avakar: Ah, I now understand your somewhat ill-expressed point. – Paul J. Lucas Jul 23 '15 at 16:46

2 Answers2

2

The former is kind of a hack. A friend class could still call the constructor (and you'd get an error at link-time unless you actually defined it as well).

The latter actually prevents its auto-generation so it's really not there.

Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
2

Example 1 was the way to do it before we had = delete which came out in C++11. Now that we have = delete this will completely get rid of the constructor. With making the constructor private you could still use that constructor in a member function where if you try to default an object in a member function with = delete it will be a compiler error.

#include <iostream>

class Foo
{
    Foo();
public:
    static void SomeFunc() { Foo f; }
};

class Bar
{
public:
    Bar() = delete;
    static void SomeFunc() { Bar b; }
};

int main()
{
    Foo::SomeFunc();  // will compile
    Bar::SomeFunc();  // compiler error
}

Live Example

NathanOliver
  • 171,901
  • 28
  • 288
  • 402