pepero pepero - 22 days ago 7
C++ Question

put disabled copy constructor in private

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?

Answer

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".