All Questions

1719
votes
8answers
202379 views

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?
1550
votes
5answers
249032 views

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 i...
87
votes
2answers
19589 views

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...
51
votes
7answers
109090 views

Dynamically allocating an array of objects

This is kind of a beginners question, but I haven't done C++ in a long time, so here goes... I have a class that contains a dynamically allocated array, say class A { int* myArray; A() {...
85
votes
8answers
39475 views

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

Why must a copy constructor be passed its parameter by reference?
112
votes
6answers
132800 views

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?
10
votes
4answers
2019 views

Why copy constructor is not called in this case?

Here is the little code snippet: class A { public: A(int value) : value_(value) { cout <<"Regular constructor" <<endl; } A(const A& other) : value_(other.va...
47
votes
6answers
28148 views

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, a...
67
votes
3answers
57594 views

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...
34
votes
2answers
22773 views

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...

Previous Next