copy-constructor

When do we have to use copy constructors?

六月ゝ 毕业季﹏ 提交于 2019-11-26 04:08:07
问题 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? 回答1: The copy constructor generated by the compiler does member-wise copying. Sometimes that is not sufficient. For example: class Class { public: Class( const char* str ); ~Class(); private: char* stored; }; Class::Class( const char* str ) { stored = new char[srtlen( str ) + 1 ]; strcpy( stored, str ); } Class::~Class() { delete[]

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

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 02:27:27
问题 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 both to use? 回答1: Yes. There are two common options. One - which is generally discouraged - is to call the operator= from the copy constructor explicitly: MyClass(const MyClass& other) { operator=(other); } However, providing a good operator= is a

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

半腔热情 提交于 2019-11-26 00:41:53
问题 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 a3; a3 = a2; //<--- this is the problematic line The operator= is defined as follows: A A::operator=(const A& a) { if (this == &a) { return *this; } param = a.param; return *this; } 回答1: Strictly speaking, the result of a copy assignment operator

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

风流意气都作罢 提交于 2019-11-26 00:25:06
问题 Why must a copy constructor be passed its parameter by reference? 回答1: Because if it's not by reference, it's by value. To do that you make a copy, and to do that you call the copy constructor. But to do that, we need to make a new value, so we call the copy constructor, and so on... (You would have infinite recursion because "to make a copy, you need to make a copy".) 回答2: Because pass-by-value would invoke the copy constructor :) 回答3: The alternative to pass-by-reference is pass-by-value.

Dynamically allocating an array of objects

拟墨画扇 提交于 2019-11-26 00:17:37
问题 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() { myArray = 0; } A(int size) { myArray = new int[size]; } ~A() { // Note that as per MikeB\'s helpful style critique, no need to check against 0. delete [] myArray; } } But now I want to create a dynamically allocated array of these classes. Here\'s my current code: A* arrayOfAs = new A[5]; for (int i = 0; i <

Why copy constructor is not called in this case?

帅比萌擦擦* 提交于 2019-11-25 23:49:59
问题 Here is the little code snippet: class A { public: A(int value) : value_(value) { cout <<\"Regular constructor\" <<endl; } A(const A& other) : value_(other.value_) { cout <<\"Copy constructor\" <<endl; } private: int value_; }; int main() { A a = A(5); } I assumed that output would be \"Regular Constructor\" (for RHS) followed by \"Copy constructor\" for LHS. So I avoided this style and always declared variable of class as A a(5); . But to my surprise in the code above copy constructor is

What is The Rule of Three?

99封情书 提交于 2019-11-25 22:50:37
问题 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? 回答1: Introduction C++ treats variables of user-defined types with value semantics . This means that objects are implicitly copied in various contexts, and we should understand what "copying an object" actually means. Let us consider a simple example: class person { std::string name; int age; public: person

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

让人想犯罪 __ 提交于 2019-11-25 22:36:34
问题 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 resource online. Can anyone help? 回答1: In the following, "auto-generated" means "implicitly declared as defaulted, but not defined as deleted". There are situations where the special member functions are declared, but defined as deleted. The

What is the copy-and-swap idiom?

我怕爱的太早我们不能终老 提交于 2019-11-25 22:09:51
问题 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 a partial list of places where it was previously mentioned: What are your favorite C++ Coding Style idioms: Copy-swap Copy constructor and = operator overload in C++: is a common function possible? What is copy elision and how it optimizes copy