copy-constructor

How can I extend a compiler generated copy constructor

廉价感情. 提交于 2019-11-30 07:54:54
问题 I frequently run into the problem, that I must extend a compiler generated copy constructor. Example: class xyz; class C { ... int a, b, c; std::set<int> mySet; xyz *some_private_ptr; }; Assume, that some_private_ptr should only be copied under certain conditions. For other conditions it should be reset to NULL on copy. So I have to write a copy constructor like: C::C(const C &other) : a(other.a), b(other.b), c(other.c), mySet(other.mySet) { if(CanCopy(other.some_private_ptr)) // matches

Can objects with private copy constructors be thrown?

China☆狼群 提交于 2019-11-30 07:13:11
问题 I've come across some exceptions issue that is unclear to me. In C++, when an object is thrown it is first copied to a temporary object, and the temporary object is then passed to the catching code. The copy involves the use of the object's class copy constructor. AFAIK, this means that if a class has a private copy constructor, it can't be used as an exception. However, in VS2010, the following code compiles and runs: class Except { Except(const Except& other) { i = 2; } public: int i;

C++ Copy constructor, temporaries and copy semantics

匆匆过客 提交于 2019-11-30 07:08:07
问题 For this program #include <iostream> using std::cout; struct C { C() { cout << "Default C called!\n"; } C(const C &rhs) { cout << "CC called!\n"; } }; const C f() { cout << "Entered f()!\n"; return C(); } int main() { C a = f(); C b = a; return 0; } the output I get is: Entered f()! Default C called! CC called! Since f() is returning by value, it should return a temporary. As T a = x; is T a(x); , wouldn't it call the copy constructor for the construction of a , with the temporary passed-in

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

ε祈祈猫儿з 提交于 2019-11-30 06:54:07
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 the half? Are there any disadvantages or warnings for this approach, compared to the Rule of Five?

Reducing code duplication between operator= and the copy constructor

强颜欢笑 提交于 2019-11-30 06:46:13
问题 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? 回答1: 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

How to declare copy constructor in derived class, without default construcor in base?

倖福魔咒の 提交于 2019-11-30 06:46:07
Please take a look on the following example: class Base { protected: int m_nValue; public: Base(int nValue) : m_nValue(nValue) { } const char* GetName() { return "Base"; } int GetValue() { return m_nValue; } }; class Derived: public Base { public: Derived(int nValue) : Base(nValue) { } Derived( const Base &d ){ std::cout << "copy constructor\n"; } const char* GetName() { return "Derived"; } int GetValueDoubled() { return m_nValue * 2; } }; This code keeps throwing me an error that there are no default contructor for base class. When I declare it everything is ok. But when i dont, code does not

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

自闭症网瘾萝莉.ら 提交于 2019-11-30 06:24:44
问题 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

Why does an in-place member initialization use a copy constructor in C++11?

萝らか妹 提交于 2019-11-30 06:18:44
I'm a little bit confused about the following code: struct A { std::atomic<int> a = 0; }; Which gives an error: copying member subobject of type 'std::atomic' invokes deleted constructor But almost the same code does work: struct A { std::atomic<int> a = {0}; }; Okey, if the first variant requires the copy constructor, then it have to use operator=() . But wait! This operator perfectly works without the copy constructor: A a; a.a = 1; Can anyone explain how both of the in-place initializations are expanded in terms of simple operations? Why the first one requires copy constructor? All

Copy Constructor is not invoked [duplicate]

北战南征 提交于 2019-11-30 05:28:52
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why copy constructor is not called in this case? Consider the sample program below: #include <iostream> using namespace std; class sample { private: int x; public: sample(int a=0) : x(a) { cout << "default ctor invoked\n"; } sample(const sample& obj) { cout << "copy ctor invoked\n"; } }; int main() { sample s2 = sample(20); //Line1 sample s3 = 20; //Line2 return 0; } In Line1 , first the constructor of sample

Copy constructor with smart pointer

三世轮回 提交于 2019-11-30 05:23:50
问题 I have a class with one std::unique_ptr as class member. I was wondering, how to correctly define the copy constructor, since I'm getting the following compiler error message: error C2248: std::unique_ptr<_Ty>::unique_ptr : cannot access private member declared in class 'std::unique_ptr<_Ty> . My class design looks something like: template <typename T> class Foo{ public: Foo(){}; Foo( Bar<T> *, int ); Foo( const Foo<T> & ); ~Foo(){}; void swap( Foo<T> & ); Foo<T> operator = ( Foo<T> );