C ++ Copy constructor by default

I understand that the compiler will not generate a default copy of ctor if copy ctor is declared private in the class.

But can someone explain why the compiler does this?

What happens if copy ctor is declared protected? Will the compiler provide a default copy of ctor?

What happens if copy ctor is declared private but has a definition, for example. foo (const & obj) {}

+3
source share
4 answers

, ( , ), , ctor. , , , , ( , , , ).

, ctor, , ( , ), , . ctor , , , , ( , , , , - ).

+4

, , . (//) .

, , . , , .

, . .

+2

$12/1 - " (12.1), (12.8) (12.4) . [ : , . , . [...]"

, , , , .

. , / , .

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

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

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

, . , (, ) .

0

Source: https://habr.com/ru/post/1767759/


All Articles