copy-constructor

What is the Rule of Four (and a half)?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 02:31:32
问题 For properly handling object copying, the rule of thumb is the Rule of Three. With C++11, move semantics are a thing, so instead it's the Rule of Five. However, in discussions around here and on the internet, I've also seen references to the Rule of Four (and a half), which is a combination of the Rule of Five and the copy-and-swap idiom. So what exactly is the Rule of Four (and a half)? Which functions need to be implemented, and what should each function's body look like? Which function is

Copy Constructor Needed with temp object

こ雲淡風輕ζ 提交于 2019-11-29 01:53:08
The following code only works when the copy constructor is available. When I add print statements (via std::cout ) and make the copy constructor available it is not used (I assume there is so compiler trick happening to remove the unnecessary copy). But in both the output operator << and the function plop() below (where I create a temporary object) I don't see the need for the copy constructor. Can somebody explain why the language needs it when I am passing everything by const reference (or what I am doing wrong). #include <iostream> class N { public: N(int) {} private: N(N const&); }; std:

What is copy/move constructor choosing rule in C++? When does move-to-copy fallback happen?

筅森魡賤 提交于 2019-11-28 21:26:51
The first example: #include <iostream> #include <memory> using namespace std; struct A { unique_ptr<int> ref; A(const A&) = delete; A(A&&) = default; A(const int i) : ref(new int(i)) { } ~A() = default; }; int main() { A a[2] = { 0, 1 }; return 0; } It works perfectly. So here the MOVE constructor is used. Let's remove the move constructor and add a copy one: #include <iostream> #include <memory> using namespace std; struct A { unique_ptr<int> ref; A(const A&a) : ref( a.ref.get() ? new int(*a.ref) : nullptr ) { } A(A&&) = delete; A(const int i) : ref(new int(i)) { } ~A() = default; }; int main

Reducing code duplication between operator= and the copy constructor

与世无争的帅哥 提交于 2019-11-28 21:25:21
I have a class that requires a non-default copy constructor and assignment operator (it contains lists of pointers). Is there any general way to reduce the code duplication between the copy constructor and the assignment operator? There's no "general way" for writing custom copy constructors and assignment operators that works in all cases. But there's an idiom called "copy-&-swap": class myclass { ... public: myclass(myclass const&); void swap(myclass & with); myclass& operator=(myclass copy) { this->swap(copy); return *this; } ... }; It's useful in many (but not all) situations. Sometimes

Checklist for writing copy constructor and assignment operator in C++

六月ゝ 毕业季﹏ 提交于 2019-11-28 19:04:46
问题 Please write a list of tasks that a copy constructor and assignment operator need to do in C++ to keep exception safety, avoid memory leaks etc. 回答1: First be sure you really need to support copy. Most of the time it is not the case, and thus disabling both is the way to go. Sometimes, you'll still need to provide duplication on a class from a polymorphic hierarchy, in that case: disable the assignment operator, write a (protected?) copy constructor, and provide a virtual clone() function.

How can I prevent a variadic constructor from being preferred to the copy constructor?

空扰寡人 提交于 2019-11-28 18:14:00
I have a template 'Foo', which owns a T, and I'd like it to have a variadic constructor that forwards its arguments to T's constructor: template<typename T> struct Foo { Foo() : t() {} Foo(const Foo& other) : t(other.t) {} template<typename ...Args> Foo(Args&&... args) : t(std::forward<Args>(args)...) {} T t; }; However, this causes Foo to not be copyable: int main(int argc, char* argv[]) { Foo<std::shared_ptr<int>> x(new int(42)); decltype(x) copy_of_x(x); // FAILS TO COMPILE return EXIT_SUCCESS; } because, according to this answer , the non-constness of the argument causes the variadic

default copy constructor

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 17:50:45
Can the (implicit) default copy constructor be called for a class that has already user-defined constructor but that is not the copy constructor ? If it is possible then, suppose we define the copy constructor for the class explicitly , now can the (implicit)default constructor be called? James Kanze First, let's clarify our vocabulary a bit. A default constructor is a constructor which can be called without any arguments. A copy constructor is a constructor which can be called with a single argument of the same type. Given this, a "default copy constructor" would be a constructor with a

Why does the implicit copy constructor calls the base class copy constructor and the defined copy constructor doesn't?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 16:12:54
Consider a class hierarchy where A is the base class and B derives from A . If the copy constructor is not defined in B , the compiler will synthesize one. When invoked, this copy constructor will call the base class copy constructor (even the synthesized one, if none has been provided by the user). #include <iostream> class A { int a; public: A() { std::cout << "A::Default constructor" << std::endl; } A(const A& rhs) { std::cout << "A::Copy constructor" << std::endl; } }; class B : public A { int b; public: B() { std::cout << "B::Default constructor" << std::endl; } }; int main(int argc,

Why is this copy constructor called rather than the move constructor?

假装没事ソ 提交于 2019-11-28 12:11:14
The following code snippet causes the copy constructor to be called where I expected the move constructor to be called: #include <cstdio> struct Foo { Foo() { puts("Foo gets built!"); } Foo(const Foo& foo) { puts("Foo gets copied!"); } Foo(Foo&& foo) { puts("Foo gets moved!"); } }; struct Bar { Foo foo; }; Bar Meow() { Bar bar; return bar; } int main() { Bar bar(Meow()); } On VS11 Beta, in debug mode, this prints: Foo gets built! Foo gets copied! Foo gets copied! I checked the standard and Bar seems to meet all requirements to have a default move constructor automatically generated, yet that

Could I have copy constructor for subclass of QObject?

拜拜、爱过 提交于 2019-11-28 11:37:31
Here we can read that no copy construct and copy assignment operator evaluable. But here we can read that qRegisterMetaType and Q_DECLARE_METATYPE have to have public default constructor, public copy constructor and public destructor. The question is: who is telling a lie? Or I did not understand it correctly? Ezee Everything is true: 1. QObject can't be copied and all its descendants can't be copied also. 2. Q_DECLARE_METATYPE accepts objects with public constructor, copy constructor and destructor. There is no contradiction, because you can't register QObject descendants with Q_DECLARE