The Wayback Machine - https://web.archive.org/web/20120425192312/http://en.cppreference.com:80/w/cpp/language/copy_constructor

Copy constructors

From cppreference.com
Jump to: navigation, search

A copy constructor is used to initialize an object from another object of the same class.
When possible and no copy constructor is defined by the user, the compiler will implicitly define a copy constructor which will call the copy constructor of all ancestors and members.
If an ancestor or member does not have a copy-constructor (this can also happen when some member is a reference).

[edit] Syntax

class_name ( const class_name & ) (1)
class_name ( const class_name & ) = default; (1)
class_name ( const class_name & ) = delete; (1)

[edit] Explanation

  1. Standard declaration of a copy constructor
  2. Forcing a copy constructor to be generated by the compiler
  3. Avoiding implicit default constructor


To make a class uncopyable in C++93, a declaration of a copy constructor must be present inside the private part of a class. It can be left unimplemented and its only purpose is to make the compiler give errors on copying:

class C
{
  private: 
    C ( const C& ); // C cannot be copied
};
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
In other languages