copy-constructor

Copy Constructor with assignment overloading syntax?

半腔热情 提交于 2019-12-11 00:21:31
问题 I am working with writing the big five(copy constructor, copy assignment operator, move constructor, move assignment operator, destructor). And I've hit a bit of a snag with the copy constructor syntax. Say I have a class foo that has the following private members: template<class data> // edit class foo{ private: int size, cursor; // Size is my array size, and cursor is the index I am currently pointing at data * dataArray; // edit } If I were to write a constructor for this of some arbitrary

Is it possible to implement a copyable_unique_ptr that is not affected by slicing?

梦想的初衷 提交于 2019-12-10 22:52:14
问题 Regardless of the fact that copying a unique_ptr makes sense or not*, I tried to implement this kind of class, simply wrapping a std::unique_ptr , and got into difficulty exactly where the copy is taken, in the case of a smart pointer to base and the stored object being a derived class. A naive implementation of the copy constructor can be found all over the internet ( data is the wrapped std::unique_ptr ): copyable_unique_ptr::copyable_unique_ptr(const copyable_unique_ptr& other) : data(std:

How to define a copy constructor for a const template parameter?

我的未来我决定 提交于 2019-12-10 21:52:27
问题 I'm creating a custom iterator and I'm having trouble satisfying the scenario where I create a const iterator and initialize it with a non- const begin() . This is legal according to the STL and can be demonstrated with std::string: #include <string> using namespace std; int main() { string::iterator a; string::const_iterator b = a; return 0; } I can't figure out how to make it work: template<typename T> class some_class { }; int main() { some_class<int> a; // Works OK! const some_class<int>

Copy or Move Constructor for a class with a member std::mutex (or other non-copyable object)?

橙三吉。 提交于 2019-12-10 19:57:28
问题 class A { private: class B { private: std::mutex mu; A* parent = NULL; public: B(A* const parent_ptr): parent(parent_ptr) {} B(const A::B & b_copy) { /* I thought I needed code here */ } }; public: B b = B(this); //...to make this copy instruction work. // (Copy constructor is deleted, need to declare a new one?) }; I have a class B that is basically a thread-safe task queue. It contains a deque , a mutex , and a condition_variable . It facilitates a consumer/producer relationship between any

Linux vs Windows std::map assignment constructors (Why such a difference?)

僤鯓⒐⒋嵵緔 提交于 2019-12-10 18:25:07
问题 I was witnessing some unexpected behavior in a C++ application I am writing in Linux Ubuntu. I would construct an object with parameters and then put a copy of that object into a std::map using the assignment operator. I wrote a simple program to demonstrate this situation... #include <iostream> #include <string> #include <map> using namespace std; class Foo { public: Foo(void) : _x(0) { cout << "Default" << endl; } Foo(int a) : _x(a) { cout << "Param" << endl; } Foo(Foo const &foo) : _x(foo.

Why deleted copy constructor doesn't let to use other constructor with polymorphic type?

随声附和 提交于 2019-12-10 17:38:27
问题 I wonder why this program doesn't compile (the same behavior on msvc, gcc and clang): #include <iostream> using namespace std; struct Action { virtual void action() { cout << "Action::action()\n"; } }; struct ActionDecorator : Action { ActionDecorator(const ActionDecorator&) = delete; ActionDecorator(Action & action) : origAction(action) { } void action() override { decoration(); origAction.action(); } private: void decoration() { cout << "ActionDecorator::decoration()\n"; } Action &

Compiler optimization or my misunderstanding

狂风中的少年 提交于 2019-12-10 17:14:41
问题 Recently I was testing some C++ deep and dark corners and I got confused about one subtle point. My test is so simple actually: // problem 1 // no any constructor call, g++ acts as a function declaration to the (howmany()) // g++ turns (howmany()) into (howmany(*)()) howmany t(howmany()); // problem 2 // only one constructor call howmany t = howmany(); My expectation from above line was; first howmany() constructor call will produce one temporary object and then compiler will use that

Why It is illegal to copy an object if a member of the class is a reference?

醉酒当歌 提交于 2019-12-10 14:58:26
问题 I met a quiz saying that the code in line 18 below is ill-formed because "It is ill-formed to use an implicitly defined assignment operator when one of the members that will need to be copied is a reference. " I couldn't understand that. Why reference could not be copied? Why Line 16 is legal? Line 16 is quite similar to line 18, a copy constructor still need to do the copy, right? 1 #include <iostream> 2 3 struct A 4 { 5 A(int& var) : r(var) {} 6 7 int &r; 8 }; 9 10 int main(int argc, char**

C++ returning an object copy

二次信任 提交于 2019-12-10 13:23:22
问题 I wrote the following code: class MyObjectHolder { public: std::vector<int> getMyObject() const { return myObject; } private: std::vector<int> myObject; }; At some point of my program I attempt to use the getMyObject method and use only const methods on the retrieved object: const std::vector<int> myObject = myObjectHolder.getMyObject(); myObject.size(); int a = myObject.front(); Now, is it possible that the compiler will optimize this code so that no copies of the std::vector<int> are done?

Conflict between perfect forwarding constructor and copy constructor in class hierarchy

拈花ヽ惹草 提交于 2019-12-10 12:58:16
问题 I recently encountered a problem while trying to implement a class hierarchy with perfect forwarding constructors. Consider the following example: struct TestBase { template<typename T> explicit TestBase(T&& t) : s(std::forward<T>(t)) {} // Compiler refers to this line in the error message TestBase(const TestBase& other) : s(other.s) {} std::string s; }; struct Test : public TestBase { template<typename T> explicit Test(T&& t) : TestBase(std::forward<T>(t)) {} Test(const Test& other) :