Leo Liu
Leo Liu

Reputation: 43

Passing argument of object without copy constructor?

I was looking into move semantics in C++11, and got to the part where something like:

SomeClass bar = createSomeClass();     //creates some object of SomeClass
foo(bar);
foo(createSomeClass());

I know that in the first foo the compiler will call SomeClass's copy constructor and the second foo the compiler will call an overloaded move constructor since createSomeClass() returns an R-value.

What if I don't have a copy constructor declared at all? How does the compiler actually know how to copy these objects then?

Upvotes: 4

Views: 1502

Answers (1)

David Scarlett
David Scarlett

Reputation: 3341

A default copy constructor will be automatically provided (performing a memberwise copy) unless the class declares a copy constructor, deletes the copy constructor, or declares a move operation. A default copy constructor will still be automatically provided is a user-declared destructor or copy assignment operator exists, but this is deprecated.

A default copy assignment operator will be automatically provided (performing a memberwise copy) unless the class declares a copy assignment operator, deletes the copy assignment operator, or declares a move operation. A default copy constructor will still be automatically provided is a user-declared destructor or copy constructor exists, but this is deprecated.

A default move constructor and move assignment operator will be automatically provided only if the class does not declare any copy operations, move operations, or a destructor.

Upvotes: 2

Related Questions