0

To disable copy constructor and assignment operator, it is clear that we could do either, since c++11:

class A {
 public:
  A(const A&) = delete;
  A& operator=(const A&) = delete;
} 

or for c++03:

class A {
 private:
  A(const A&);
  A& operator=(const A&);
}

however, what happens with this:

class A {
 private:
  A(const A&) = delete;
  A& operator=(const A&) = delete;
}

i guess this also leads to the same result. Is there any side effect?

pepero
  • 7,095
  • 7
  • 41
  • 72
  • 1
    Why don't you try it, and see what happens. – Sam Varshavchik Nov 15 '16 at 11:55
  • Possible duplicate of [c++ syntax: default and delete modifiers](http://stackoverflow.com/q/16770492/636019) and/or [With explicitly deleted member functions in C++11, is it still worthwhile to inherit from a noncopyable base class?](http://stackoverflow.com/q/9458741/636019) – ildjarn Nov 15 '16 at 12:02
  • I tried. all the three work the same. – pepero Nov 15 '16 at 12:21

1 Answers1

4

It doesn't matter what access you give a deleted function - it simply doesn't exist(¹), so it is inaccessible whatever the caller.

The error messages may be slightly more confusing. See for example http:://cpp.sh/9hv7y where the first error is about "private" rather than "deleted".


¹ "it doesn't exist" is a simplification. It exists in the sense that it participates in overload resolution, but it is an error if it is the selected function. Thus

struct only_double {
    only_double(intmax_t) = delete;
    only_double(double arg);
};
only_double zero(0); // Error - deleted constructor called
  • yeah, delete function actually does not exist, so accessor does not matter. this explains. Thanks! – pepero Nov 15 '16 at 12:25
  • I don't guess it's correct to say that _it doesn't exist_. Note that - _The one-definition rule applies to deleted definitions_. See [here](http://eel.is/c++draft/dcl.fct.def.delete#4) for further details. – skypjack Nov 15 '16 at 13:40
  • @skypjack: Yup. It was true in the context of the question, but I've added a footnote (which ends up longer than the real answer!) – Martin Bonner supports Monica Nov 15 '16 at 16:15