copy-constructor

What constructor or operator is used in a return (C++)

烈酒焚心 提交于 2021-02-07 19:17:06
问题 I run this code for experimenting copy constructor and assignment operator class AClass { private: int a; public: AClass (int a_) : a(a_) { cout << " constructor AClass(int) " << a << endl; } AClass(const AClass & x) : a(x.a) { cout << " copy constructor AClass(const AClass &) " << a << endl; } AClass & operator=(const AClass & x) { a = x.a; cout << " AClass& operator=(const AClass &) " << a - endl; return *this; } }; AClass g () { AClass x(8); return x; } int main () { cout << " before

Should I declare the copy constructor of my exceptions noexcept?

本小妞迷上赌 提交于 2021-02-07 11:52:45
问题 In More Effective C++ , Scott Meyers says C++ specifies that an object thrown as an exception is copied. I suppose then, that if the copy constructor throws an exception in turn, std::terminate is called, so this is a good reason for declaring all my exceptions' copy constructors noexcept (and also, I guess, to not throw objects which allocate memory from the heap, like std::string ). Yet I was surprised to see that the standard library implementation shipped with GCC 4.7.1 doesn’t define

C++ copies of objects with abstract class pointers

删除回忆录丶 提交于 2021-02-07 07:59:34
问题 Consider the Container class, which basically stores a vector of unique_ptr s of Box objects and can perform some computations on them. class Container { private: std::vector<std::unique_ptr<Box> > boxes_; public: Container(std::vector<std::unique_ptr<Box> > &&boxes): boxes_(std::move(boxes)){} double TotalVolume() { /* Iterate over this->boxes_ and sum */ } }; Here, Box is an abstract class that has a pure virtual method such as double Box::Volume() . Now, suppose that I instantiate a

C++ copies of objects with abstract class pointers

你说的曾经没有我的故事 提交于 2021-02-07 07:55:10
问题 Consider the Container class, which basically stores a vector of unique_ptr s of Box objects and can perform some computations on them. class Container { private: std::vector<std::unique_ptr<Box> > boxes_; public: Container(std::vector<std::unique_ptr<Box> > &&boxes): boxes_(std::move(boxes)){} double TotalVolume() { /* Iterate over this->boxes_ and sum */ } }; Here, Box is an abstract class that has a pure virtual method such as double Box::Volume() . Now, suppose that I instantiate a

Does C++11 standard require implementers to prioritize noexcept move constructor over const copy constructor for std::vector?

点点圈 提交于 2021-02-07 07:16:53
问题 Reading this and this and 23.3.6.5/1 of the standard, where in the latest C++ standard draft is it specified that implementers should prioritize the use of non-throwing move-constructor T(T &&t) noexcept over a const copy-constructor T(const T &t) when std::vector<T> re-allocates its element as a result of a push_back operation? Is it 13.3.3.1.4/1 on overload resolution of reference binding? EDIT 1 I argue on 13.3.3.1.4/1 because of the following reasons: 13.3/2 Overload resolution selects

Does C++11 standard require implementers to prioritize noexcept move constructor over const copy constructor for std::vector?

时光怂恿深爱的人放手 提交于 2021-02-07 07:16:37
问题 Reading this and this and 23.3.6.5/1 of the standard, where in the latest C++ standard draft is it specified that implementers should prioritize the use of non-throwing move-constructor T(T &&t) noexcept over a const copy-constructor T(const T &t) when std::vector<T> re-allocates its element as a result of a push_back operation? Is it 13.3.3.1.4/1 on overload resolution of reference binding? EDIT 1 I argue on 13.3.3.1.4/1 because of the following reasons: 13.3/2 Overload resolution selects

Copy vector of vectors in copy constructor

六月ゝ 毕业季﹏ 提交于 2021-01-29 03:13:35
问题 A simple thing as I thought at first seems to be harder than I thought. I want to copy a vector of vectors of type int inside a copy constructor. std::vector<std::vector<int> * > * bar; I tried this but it is not working: Foo(const Foo& rhs) : bar(new std::vector<std::vector<int> * >(rhs.vec->size())) { for (std::size_t i = 0; i < rhs.bar->size(); i++) { bar->push_back(new std::vector<int>()); for (size_t j = 0; j < (*rhs.bar)[i]->size(); j++) { bar->back()->push_back((*rhs.bar)[i]->at(j)); }

What does a non-trivial copy constructor do? [duplicate]

泪湿孤枕 提交于 2021-01-28 03:17:07
问题 This question already has answers here : What is a non-trivial constructor in C++? (3 answers) Closed 5 years ago . In C++, if a copy constructor is not defined the compiler will do that for you. If one is defined, compiler would not. The compiler generated copy constructor can be trivial or non-trivial. In a trivial copy constructor it does a member-wise copy. That's it. However, if there is a virtual function, the copy constructor is non-trivial. It cannot just to bit-wise copy. So here is

How do I allow move construction and disallow assignment and copy construction of a class

怎甘沉沦 提交于 2021-01-27 04:42:11
问题 Is there a way to allow a move constructor and disallow copy construction and assignment. I can think of several classes with file pointers and buffer pointers (resource handles etc) that would benefit from being copy constructed and assigned. I am using VC2010 & GCC 4.5.2. I know that I would have to declare empty private assignment and copy constructors in the VC2010 class headers and as far as I am aware GCC allows some sort of delete signature after the method to do the same thing. If

How do I allow move construction and disallow assignment and copy construction of a class

≡放荡痞女 提交于 2021-01-27 04:40:55
问题 Is there a way to allow a move constructor and disallow copy construction and assignment. I can think of several classes with file pointers and buffer pointers (resource handles etc) that would benefit from being copy constructed and assigned. I am using VC2010 & GCC 4.5.2. I know that I would have to declare empty private assignment and copy constructors in the VC2010 class headers and as far as I am aware GCC allows some sort of delete signature after the method to do the same thing. If