copy-constructor

Does guaranteed copy elision work with function parameters?

六眼飞鱼酱① 提交于 2019-12-21 13:12:13
问题 If I understood correctly, starting from C++17, this code now requires that no copy will be done: Foo myfunc(void) { return Foo(); } auto foo = myfunc(); // no copy Is it also true for function arguments? Will copies be optimized away in the following code? Foo myfunc(Foo foo) { return foo; } auto foo = myfunc(Foo()); // will there be copies? 回答1: In C++17, prvalues ("anonymous temporaries") are no longer objects. Instead, they are instructions on how to construct an object. They can

Does guaranteed copy elision work with function parameters?

隐身守侯 提交于 2019-12-21 13:10:04
问题 If I understood correctly, starting from C++17, this code now requires that no copy will be done: Foo myfunc(void) { return Foo(); } auto foo = myfunc(); // no copy Is it also true for function arguments? Will copies be optimized away in the following code? Foo myfunc(Foo foo) { return foo; } auto foo = myfunc(Foo()); // will there be copies? 回答1: In C++17, prvalues ("anonymous temporaries") are no longer objects. Instead, they are instructions on how to construct an object. They can

Is it possible to define an std::thread and initialize it later?

时光毁灭记忆、已成空白 提交于 2019-12-21 07:14:08
问题 My aim is to keep an std::thread object as data member, and initialize it when needed. I'm not able to do this (as in my code below) because the copy constructor of the std::thread class is deleted. Is there any other way to do it? class MyClass { public: MyClass():DiskJobThread(){}; ~MyClass(); void DoDiskJobThread(); private: int CopyThread(const std::wstring & Source, const std::wstring & Target); int MoveThread(const std::wstring & Source, const std::wstring & Target); std::thread

Self destruction: this->MyClass::~MyClass() vs. this->~MyClass()

岁酱吖の 提交于 2019-12-21 03:11:22
问题 In my quest to learn C++ I stumbled across the article Writing Copy Constructors and Assignment Operators which proposes a mechanism to avoid code duplication across copy constructors and assignment operators. To summarise/duplicate the content of that link, the proposed mechanism is: struct UtilityClass { ... UtilityClass(UtilityClass const &rhs) : data_(new int(*rhs_.data_)) { // nothing left to do here } UtilityClass &operator=(UtilityClass const &rhs) { // // Leaves all the work to the

Implementing the copy constructor in terms of operator=

扶醉桌前 提交于 2019-12-20 10:27:05
问题 If the operator= is properly defined, is it OK to use the following as copy constructor? MyClass::MyClass(MyClass const &_copy) { *this = _copy; } 回答1: If all members of MyClass have a default constructor, yes. Note that usually it is the other way around: class MyClass { public: MyClass(MyClass const&); // Implemented void swap(MyClass&) throw(); // Implemented MyClass& operator=(MyClass rhs) { rhs.swap(*this); return *this; } }; We pass by value in operator= so that the copy constructor

Construct object with itself as reference?

风流意气都作罢 提交于 2019-12-20 09:55:22
问题 I just realised that this program compiles and runs (gcc version 4.4.5 / Ubuntu): #include <iostream> using namespace std; class Test { public: // copyconstructor Test(const Test& other); }; Test::Test(const Test& other) { if (this == &other) cout << "copying myself" << endl; else cout << "copying something else" << endl; } int main(int argv, char** argc) { Test a(a); // compiles, runs and prints "copying myself" Test *b = new Test(*b); // compiles, runs and prints "copying something else" }

Does copy of an object through copy constructor get the same instance variable as the original object?

我的梦境 提交于 2019-12-20 07:30:21
问题 Does a copy of an object with object instance variables get the same instance variable as the original object? if so, I was wondering if the original and copy objects are referencing to the same instance variables. 回答1: Unlike C++, Java does not provide copy constructors automatically. There is therefore no general answer to any question about the behavior of copy constructors, as Java places no restrictions on their behavior. Nevertheless, every object, however initialized, has its own

Calling constructor from copy constructor

孤街醉人 提交于 2019-12-20 06:16:22
问题 From c++ 11 we can call a constructor from another constructor. So instead of defining copy constructor can we call the constructor every time? Like in this piece of code : class MyString { private: char *ptr; int m_length; public: MyString(const char *parm = nullptr) : m_length(0), ptr(nullptr) { if (parm) { m_length = strlen(parm) + 1; ptr = new char[m_length]; memcpy(ptr, parm, m_length); } } MyString(const MyString &parm) : MyString(parm.ptr) { } }; Is there any ill effect to this

explicit copy constructor ignored even if exact argument was provided

拟墨画扇 提交于 2019-12-20 04:50:01
问题 The copy constructor has been provided. While using it exactly same type is passed to argument. Still the it seems the compiler( gcc/g++ 4.8.2) ignores existence of explicit copy constructor. The code generates compilation error. Why? The error is: t.cpp: In function ‘A f(const A&)’: t.cpp:19:12: error: no matching function for call to ‘A::A(const A&)’ return a; //compilation error with gcc 4.8.2 ^ t.cpp:19:12: note: candidate is: t.cpp:14:5: note: A::A() A(){} ^ t.cpp:14:5: note: candidate

Why can't I initialize an array of objects if they have private copy constructors?

99封情书 提交于 2019-12-20 03:51:03
问题 I just ran across some unexpected and frustrating behaviour while working on a C++ project. My actual code is a tad more complicated, but the following example captures it just as well: class Irritating { public: Irritating() {} private: Irritating(const Irritating& other) {} }; const Irritating singleton; // Works just fine. const Irritating array[] = {Irritating()}; // Compilation error. int main() { return 0; } Trying to compile this produces the following error (GCC version thrown in just