copy-constructor

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

有些话、适合烂在心里 提交于 2019-11-27 06:51:02
问题 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

Python: Implementation of shallow and deep copy constructors

China☆狼群 提交于 2019-11-27 05:59:14
问题 It is in most of the situations easy to implement copy constructors (or overloaded assignment operator) in C++ since there is a concept of pointers. However, I'm quite confused about how to implement shallow and deep copy in Python. I know that there are special commands in one of the libraries but they don't work on classes that you have written by yourself. So what are the common ways to implement? P.S. Showing process on some basic data structures (linked list or tree) will be appreciated.

Are variadic constructors supposed to hide the implicitly generated ones?

依然范特西╮ 提交于 2019-11-27 05:44:22
问题 Are variadic constructors supposed to hide the implicitly generated ones, i.e. the default constructor and the copy constructor? struct Foo { template<typename... Args> Foo(Args&&... x) { std::cout << "inside the variadic constructor\n"; } }; int main() { Foo a; Foo b(a); } Somehow I was expecting this to print nothing after reading this answer, but it prints inside the variadic constructor twice on g++ 4.5.0 :( Is this behavior correct? It also happens without variadic templates: struct Foo

Copy Constructor in C++ is called when object is returned from a function?

我是研究僧i 提交于 2019-11-27 05:16:59
问题 I understand copy constructor is called on three instances When instantiating one object and initializing it with values from another object. When passing an object by value. 3. When an object is returned from a function by value. I have question with no.3 if copy constructor is called when an object value is returned, shouldn't it create problems if object is declared locally in the function. i mean the copy constructor is a deep copy one and takes reference of an object as parameter 回答1: It

Explicit copy constructor

痞子三分冷 提交于 2019-11-27 05:16:35
I have extended std::string to fulfil my needs of having to write custom function build into string class called CustomString I have defined constructors: class CustomString : public std::string { public: explicit CustomString(void); explicit CustomString(const std::string& str); explicit CustomString(const CustomString& customString); //assignment operator CustomString& operator=(const CustomString& customString); ... }; In the third constructor (copy constructor) and assignment operator, whose definition is: CustomString::CustomString(const CustomString& customString): std::string(static

C++ Compiler Error C2280 “attempting to reference a deleted function” in Visual Studio 2013 and 2015

北城以北 提交于 2019-11-27 04:29:55
This snippet is compiled without errors in Visual Studio 2013 (Version 12.0.31101.00 Update 4) class A { public: A(){} A(A &&){} }; int main(int, char*) { A a; new A(a); return 0; } while it is compiled with this error in Visual Studio 2015 RC (Version 14.0.22823.1 D14REL): 1>------ Build started: Project: foo, Configuration: Debug Win32 ------ 1> foo.cpp 1>c:\dev\foo\foo.cpp(11): error C2280: 'A::A(const A &)': attempting to reference a deleted function 1> c:\dev\foo\foo.cpp(6): note: compiler has generated 'A::A' here ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========

Why is copy constructor not being called in this code

為{幸葍}努か 提交于 2019-11-27 04:08:16
问题 So why is Copy constructor not being invoked in " const Integer operator+(const Integer &rv) " function. Is it because of RVO. If Yes what do I need to do to prevent it? #include <iostream> using namespace std; class Integer { int i; public: Integer(int ii = 0) : i(ii) { cout << "Integer()" << endl; } Integer(const Integer &I) { cout << "Integer(const Integer &)" << endl; } ~Integer() { cout << "~Integer()" << endl; } const Integer operator+(const Integer &rv) const { cout << "operator+" <<

Calling assignment operator in copy constructor

不羁的心 提交于 2019-11-27 03:57:29
Are there some drawbacks of such implementation of copy-constructor? Foo::Foo(const Foo& i_foo) { *this = i_foo; } As I remember, it was recommend in some book to call copy constructor from assignment operator and use well-known swap trick, but I don't remember, why... Yes, that's a bad idea. All member variables of user-defined types will be initialized first, and then immediately overwritten. That swap trick is this: Foo& operator=(Foo rhs) // note the copying { rhs.swap(*this); //swap our internals with the copy of rhs return *this; } // rhs, now containing our old internals, will be

Why is copy constructor called instead of conversion constructor?

谁都会走 提交于 2019-11-27 03:41:26
So basically this code: class A { }; class B { B (const B& b) {} public: B (){} B (const A& a) {} }; int main() { A a; B b1(a); //OK B b2 = a; //Error } only generates an error for B b2 = a . And that error is error: ‘B::B(const B&)’ is private Why is it attempting to call the copy constructor in addition to the direct conversion constructor? It's clear from the error message that a temporary B is created which is then used for copy-construction, but why? Where is this in the standard? B b2 = a; This is known as Copy Initialization . It does thh following: Create an object of type B from a by

What's the use of the private copy constructor in c++

北战南征 提交于 2019-11-27 03:30:42
Why do people define a private copy constructor? When is making the copy constructor and the assignment operator private a good design? If there are no members in the class which are pointers or handles to a unique object (like file name), then wat other cases are there where private copy constructor is a good idea? Same question apply for assignment operator. Given that majority of C++ revolves around copying of objects and passing by reference, are there any good designs which involve private copy constructor? Some objects represent particular entities that can't or shouldn't be copied. For