The Wayback Machine - https://web.archive.org/web/20211009194249/https://www.geeksforgeeks.org/g-fact-13/
Skip to content
Related Articles

Related Articles

Improve Article

When is copy constructor called?

  • Difficulty Level : Easy
  • Last Updated : 29 May, 2017

In C++, a Copy Constructor may be called in following cases:
1. When an object of the class is returned by value.
2. When an object of the class is passed (to a function) by value as an argument.
3. When an object is constructed based on another object of the same class.
4. When compiler generates a temporary object.

It is however, not guaranteed that a copy constructor will be called in all these cases, because the C++ Standard allows the compiler to optimize the copy away in certain cases, one example being the return value optimization (sometimes referred to as RVO).

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.

References:
http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html
http://en.wikipedia.org/wiki/Copy_constructor

My Personal Notes arrow_drop_up
Recommended Articles
Page :