copy-assignment

Why does operator = return *this?

落花浮王杯 提交于 2020-05-25 11:35:05
问题 Say I want to override the operator = so I can do something like Poly p1; // an object representing a polynomial Poly p2; // another object of the same type p2 = p1; // assigns all the contents of p1 to p2 Then in my implementation of the operator = , I have something like this: Poly& Poly::operator=(const Poly &source) { // Skipping implementation, it already works fine… return *this; } Don't mind the implementation, it already works fine. My concern is that what happens when you return

Why does operator = return *this?

允我心安 提交于 2020-05-25 11:34:05
问题 Say I want to override the operator = so I can do something like Poly p1; // an object representing a polynomial Poly p2; // another object of the same type p2 = p1; // assigns all the contents of p1 to p2 Then in my implementation of the operator = , I have something like this: Poly& Poly::operator=(const Poly &source) { // Skipping implementation, it already works fine… return *this; } Don't mind the implementation, it already works fine. My concern is that what happens when you return

Could I have copy constructor for subclass of QObject?

喜你入骨 提交于 2020-01-09 10:53:19
问题 Here we can read that no copy construct and copy assignment operator evaluable. But here we can read that qRegisterMetaType and Q_DECLARE_METATYPE have to have public default constructor, public copy constructor and public destructor. The question is: who is telling a lie? Or I did not understand it correctly? 回答1: Everything is true: 1. QObject can't be copied and all its descendants can't be copied also. 2. Q_DECLARE_METATYPE accepts objects with public constructor, copy constructor and

Why doesn't this vector assignment work?

倾然丶 夕夏残阳落幕 提交于 2019-12-24 15:14:44
问题 Similar Questions: STL vector reserve() and copy() std::vector reserve() and push_back() is faster than resize() and array index, why? std::vector::resize() vs. std::vector::reserve() #include <vector> #include <iostream> using namespace std; int main() { vector<vector<int> > vvi; vvi.resize(1); vvi[0].reserve(1); vvi[0][0] = 1; vector<int> vi = vvi[0]; cout << vi[0]; // cout << vvi[0][0]; works return 0; } This gives me a seg fault, and I can't tell why. 回答1: vvi[0].reserve(1); vvi[0][0] = 1

Implementation supplied copy constructor and assignment operator

拟墨画扇 提交于 2019-12-22 11:26:41
问题 I have a small confusion regarding the situations where the implementation (compiler) will not supply the copy constructor and the copy assignment operator. When we declare the copy ctor and/or copy assignment operator in our class. Some says when we derive from a class which has a private copy ctor and/or copy assignment operator. I am a little confused about the second situation, is the second situation is precisely. a) The implementation will not declare them for you, so you will get a

Default copy assignment with array members

久未见 提交于 2019-12-22 04:30:15
问题 I've got a class definition similar to the following: class UUID { public: // Using implicit copy assignment operator private: unsigned char buffer[16]; }; I've just had a unit test fail on me that was verifying that copy assignment worked properly. To my surprise, one byte in the middle of the buffer[] array was copied incorrectly. My understanding is that the default copy assignment operator performs memberwise copy, and that for array members (not pointer-to-array members) that entails

Can I write both copy and move assignment operators for a class?

孤街浪徒 提交于 2019-12-13 12:15:56
问题 These are my prototypes, MyClass& operator=(MyClass rhs); // copy assignment MyClass& operator=(MyClass &&rhs); // move assignment But when I call MyClass a, b; a = std::move(b); , there is an error. 556 IntelliSense: more than one operator "=" matches these operands: function "MyClass::operator=(MyClass rhs)" function "MyClass::operator=(MyClass &&rhs)" operand types are: MyClass = MyClass And the compiler returns: Error 56 error C2593: 'operator =' is ambiguous 回答1: Overload resolution is

Is there a better way to assign a new value to a numpy array scalar?

亡梦爱人 提交于 2019-12-13 09:33:48
问题 I am doing some quick calculations on a scalar value from a numpy array. As it says in the documentation, The primary advantage of using array scalars is that they preserve the array type (Python may not have a matching scalar type available, e.g. int16)... But is there a better (faster, and more concise) way of assigning a new value to an existing array scalar than this: >>> x = np.array(2.0, dtype='float32') which works but is not that convenient (I am doing other arithmetic and want to

Allocator propagation policies in your new modern C++ containers

依然范特西╮ 提交于 2019-12-08 10:52:53
问题 What is the reason for having these traits in a container (https://en.cppreference.com/w/cpp/memory/allocator_traits) propagate_on_container_copy_assignment Alloc::propagate_on_container_copy_assignment if present, otherwise std::false_type propagate_on_container_move_assignment Alloc::propagate_on_container_move_assignment if present, otherwise std::false_type propagate_on_container_swap Alloc::propagate_on_container_swap if present, otherwise std::false_type is_always_equal(since C++17)

Implementation supplied copy constructor and assignment operator

∥☆過路亽.° 提交于 2019-12-06 08:42:55
I have a small confusion regarding the situations where the implementation (compiler) will not supply the copy constructor and the copy assignment operator. When we declare the copy ctor and/or copy assignment operator in our class. Some says when we derive from a class which has a private copy ctor and/or copy assignment operator. I am a little confused about the second situation, is the second situation is precisely. a) The implementation will not declare them for you, so you will get a compile time error. OR b) The implementation will declare and define them, but when the compiler defined