Questions tagged [copy-constructor]

A copy constructor is a constructor that creates a new object that is a clone of an existing object. The term is mostly used in the C++ programming language, where copy constructors have a special status.

A copy constructor is a special constructor in the C++ programming language for creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed (const or non-const), which might be followed by parameters of any type (all having default values).

Normally the compiler automatically creates a copy constructor for each class (known as a default copy constructor) but for special cases the programmer creates the copy constructor, known as a user-defined copy constructor. In such cases, the compiler does not create one. Hence, there is always one copy constructor that is either defined by the user or by the system.


2398 questions
8 answers

What is The Rule of Three?

What does copying an object mean? What are the copy constructor and the copy assignment operator? When do I need to declare them myself? How can I prevent my objects from being copied?
  • 256,549
  • 94
  • 388
  • 662
5 answers

What is the copy-and-swap idiom?

What is the copy-and-swap idiom and when should it be used? What problems does it solve? Does it change for C++11? Related: What are your favorite C++ Coding Style idioms: Copy-swap Copy constructor and = operator overload in C++: is a common…
  • 494,350
  • 52
  • 494
  • 543
3 answers

Disable copy constructor

I have a class: class SymbolIndexer { protected: SymbolIndexer ( ) { } public: static inline SymbolIndexer & GetUniqueInstance ( ) { static SymbolIndexer uniqueinstance_ ; return uniqueinstance_ ; } }; How should I modify it to…
Humble Debugger
  • 4,439
  • 11
  • 39
  • 56
3 answers

Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator?

I want to refresh my memory on the conditions under which a compiler typically auto generates a default constructor, copy constructor and assignment operator. I recollect there were some rules, but I don't remember, and also can't find a reputable…
6 answers

Clone() vs Copy constructor- which is recommended in java

clone method vs copy constructor in java. which one is correct solution. where to use each case?
  • 14,720
  • 22
  • 68
  • 93
10 answers

Why should the copy constructor accept its parameter by reference in C++?

Why must a copy constructor's parameter be passed by reference?
  • 6,694
  • 20
  • 61
  • 71
3 answers

Copy constructor and = operator overload in C++: is a common function possible?

Since a copy constructor MyClass(const MyClass&); and an = operator overload MyClass& operator = (const MyClass&); have pretty much the same code, the same parameter, and only differ on the return, is it possible to have a common function for them…
  • 16,256
  • 15
  • 86
  • 137
7 answers

When do we have to use copy constructors?

I know that C++ compiler creates a copy constructor for a class. In which case do we have to write a user-defined copy constructor? Can you give some examples?
  • 4,342
  • 11
  • 46
  • 56
8 answers

Why must the copy assignment operator return a reference/const reference?

In C++, the concept of returning reference from the copy assignment operator is unclear to me. Why can't the copy assignment operator return a copy of the new object? In addition, if I have class A, and the following: A a1(param); A a2 = a1; A…
  • 1,886
  • 1
  • 24
  • 43
10 answers

How do I make this C++ object non-copyable?

See title. I have: class Foo { private: Foo(); public: static Foo* create(); } What need I do from here to make Foo un-copyable?
  • 41,035
  • 53
  • 197
  • 293
9 answers

Why doesn't Java have a copy constructor?

Why doesn't Java support a copy constructor like in C++?
  • 17,668
  • 31
  • 111
  • 166
7 answers

Dynamically allocating an array of objects

I have a class that contains a dynamically allocated array, say class A { int* myArray; A() { myArray = 0; } A(int size) { myArray = new int[size]; } ~A() { // Note that as per MikeB's…
  • 110,262
  • 41
  • 219
  • 271
6 answers

The copy constructor and assignment operator

If I override operator= will the copy constructor automatically use the new operator? Similarly, if I define a copy constructor, will operator= automatically 'inherit' the behavior from the copy constructor?
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
7 answers

C++ Compiler Error C2280 "attempting to reference a deleted function" in Visual Studio 2013 and 2015

This snippet is compiled without errors in Visual Studio 2013 (Version 12.0.31101.00 Update 4) class A { public: A(){} A(A &&){} }; int main(int, char*) { A a; new A(a); return 0; } while it is compiled with this error in Visual…
Alessandro Jacopson
  • 18,047
  • 15
  • 98
  • 153
3 answers

What is a converting constructor in C++ ? What is it for?

I have heard that C++ has something called "conversion constructors" or "converting constructors". What are these, and what are they for? I saw it mentioned with regards to this code: class MyClass { public: int a, b; MyClass( int i )…
  • 25,609
  • 37
  • 148
  • 229
2 3
99 100