avinash
avinash

Reputation: 33

C++ Default copy constructor

I understand compiler won't generate default copy ctor if copy ctor is declared private in a class.

But can someone explain why compiler does that?

What happens if copy ctor is declared protected? Would compiler provide default copy ctor?

What happens if copy ctor is declared private but have a definition e.g. foo(const& obj){}

Upvotes: 3

Views: 3253

Answers (4)

rkellerm
rkellerm

Reputation: 5512

The compiler won't generate a default copy constructor whenever the copy constructor is declared explicitly. That's true no matter what privacy level (private, protected or public) the explicit declaration has.

Upvotes: 1

Chubsdad
Chubsdad

Reputation: 25487

$12/1 - "The default constructor (12.1), copy constructor and copy assignment operator (12.8), and destructor (12.4) are special member functions. [ Note: The implementation will implicitly declare these member functions for some class types when the program does not explicitly declare them. The implementation will implicitly define them if they are used.[...]"

So in case since the copy constructor is declared explicitly, the compiler takes it as an intention of having a customized copy constructor and the implicit copy constructor generation is suppressed.

Copy constructor can be declared and defined as private. If the copy constructor is defined as private, copy initialization/direct initialization won't work as shown below.

struct A{
    A(){}
private:
    A(A const &){}
};

int main(){
    A a1;
    A a2(a1);  // direct initialization, error

    A a3 = a1; // copy initialization, error
}

Upvotes: 2

kennytm
kennytm

Reputation: 523184

The compiler knows a copy constructor exists, so it won't generate one. The accessibility (public / private / protected) or whether it has a definition aren't considered in this phase.

It sounds like there is no copy constructor just because you cannot call a private function from outside and non-friends. The user-defined constructor still exists, only that it is private.

If it's protected then only subclasses and itself can call the copy constructor. There will be no implicitly defined copy constructors either.

Upvotes: 3

Alex Martelli
Alex Martelli

Reputation: 881497

Any copy constructor declared in the class (be it private, public or protected) means the compiler will not generate a default copy ctor. Whether the one declared in the class is then also defined or not only controls whether code with the proper level of visibility into it can copy instances of the class (if not defined, the linker will complain; the compiler's job is only to complain about use without proper visibility, not to duplicate the linker's job).

For example, if you declare a private copy ctor, only code that is in functions in the class (or friends, of course) is allowed to compile if it tries copying an instance. If the ctor is not defined, that code, however, will not survive the linker, so you get an error anyway (just unfortunately a bit later in the build process, i.e. possibly with a modest waste of computational resources at build time compared with earlier-detected errors).

Upvotes: 5

Related Questions