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.

Source: http://en.wikipedia.org/wiki/Copy_constructor

2175 questions
2284
votes
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?
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
2138
votes
5 answers

What is the copy-and-swap idiom?

What is this idiom and when should it be used? Which problems does it solve? Does the idiom change when C++11 is used? Although it's been mentioned in many places, we didn't have any singular "what is it" question and answer, so here it is. Here is…
GManNickG
  • 459,504
  • 50
  • 465
  • 534
191
votes
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,079
  • 11
  • 36
  • 49
140
votes
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?
Jothi
  • 13,342
  • 20
  • 59
  • 92
135
votes
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…
131
votes
8 answers

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

Why must a copy constructor's parameter be passed by reference?
Jony
  • 6,352
  • 19
  • 56
  • 69
90
votes
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…
MPelletier
  • 15,130
  • 14
  • 79
  • 128
88
votes
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?
penguru
  • 3,928
  • 11
  • 41
  • 55
73
votes
6 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…
bks
  • 1,696
  • 1
  • 21
  • 43
71
votes
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? Thanks!
anon
  • 36,629
  • 47
  • 184
  • 286
70
votes
9 answers

Why doesn't Java have a copy constructor?

Why doesn't Java support a copy constructor like in C++?
Cuga
  • 16,782
  • 29
  • 106
  • 156
60
votes
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…
Domenic
  • 100,627
  • 39
  • 205
  • 257
55
votes
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 )…
kiriloff
  • 22,522
  • 32
  • 127
  • 207
52
votes
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
  • 27,853
  • 27
  • 109
  • 192
49
votes
4 answers

Do the padding bytes of a POD type get copied?

Suppose I have a POD type like this: struct A { char a; int b; }; On my system, sizeof(A) == 8, even though sizeof(char) == 1 and sizeof(b) == 4. This means that the data structure has 3 unused bytes. Now suppose we do A x = ...; A y…
Szabolcs
  • 21,860
  • 5
  • 72
  • 158
1
2 3
99 100