copy-assignment

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

左心房为你撑大大i 提交于 2019-12-04 17:06:45
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 Overload resolution is ambiguous because when you pass an rvalue, both MyClass and MyClass && can be directly initialised by it. If

python assignment in array vs scalar

こ雲淡風輕ζ 提交于 2019-12-02 07:11:52
问题 I have a 2D array A of shape (4,3) , and a 1D array a of shape (4,) . I want to swap the first two rows of A , as well as the first two elements in a . I did the following: A[0,:],A[1,:] = A[1,:],A[0,:] a[0],a[1] = a[1],a[0] Apparently, it works for a , but fails for A . Now, the second row becomes the first row, but the first row remains unchanged. If I do the following: first_row_copy = A[0,:].copy() A[0,:] = A[1,:] A[1,:] = first_row_copy Then, it seems to work. Why the first method doesn

python assignment in array vs scalar

北城余情 提交于 2019-12-02 04:53:26
I have a 2D array A of shape (4,3) , and a 1D array a of shape (4,) . I want to swap the first two rows of A , as well as the first two elements in a . I did the following: A[0,:],A[1,:] = A[1,:],A[0,:] a[0],a[1] = a[1],a[0] Apparently, it works for a , but fails for A . Now, the second row becomes the first row, but the first row remains unchanged. If I do the following: first_row_copy = A[0,:].copy() A[0,:] = A[1,:] A[1,:] = first_row_copy Then, it seems to work. Why the first method doesn't work? (but works for a ) Also, what's the difference between A_copy = A[0,:].copy() and A_copy = A[0,

Object assignment in Ruby [closed]

只愿长相守 提交于 2019-11-29 08:25:40
Coming from a c++ background I'm curious about object assignment in Ruby. What considerations (if any) should be made for the following object assignments: class MyClass attr_accessor :a, :b def initialize(a, b) @a = a @b = b end def some_method puts "#{self.a} #{self.b}" end end m = MyClass.new("first", "last") n = MyClass.new("pizza", "hello") q = n q.some_method If you're familiar with C++, then you might want to consider every variable in Ruby, instance or otherwise, as a reference to another object. Since everything in Ruby is an object, even nil , which is of type NilClass, this holds

Could I have copy constructor for subclass of QObject?

拜拜、爱过 提交于 2019-11-28 11:37:31
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? Ezee 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 destructor. There is no contradiction, because you can't register QObject descendants with Q_DECLARE

Object assignment in Ruby [closed]

一笑奈何 提交于 2019-11-28 01:52:25
问题 Coming from a c++ background I'm curious about object assignment in Ruby. What considerations (if any) should be made for the following object assignments: class MyClass attr_accessor :a, :b def initialize(a, b) @a = a @b = b end def some_method puts "#{self.a} #{self.b}" end end m = MyClass.new("first", "last") n = MyClass.new("pizza", "hello") q = n q.some_method 回答1: If you're familiar with C++, then you might want to consider every variable in Ruby, instance or otherwise, as a reference

When is the copy assignment operator called?

守給你的承諾、 提交于 2019-11-28 00:26:20
问题 When I read about copy constructor and copy assignment constructor, what I understood is that both gives away their properties one to another, and that both of them are declared implicitly by compiler (if not defined). So both must be exist whether doing something useful or not. Then I tested this code: #include <iostream> using namespace std; class Test { public: Test () { cout << "Default constructor" << endl; } Test (const Test& empty) { cout << "Copy constructor" << endl; } Test& operator

Why are arrays not assignable in C/C++? [duplicate]

拜拜、爱过 提交于 2019-11-27 23:26:39
问题 This question already has an answer here: Why do C and C++ support memberwise assignment of arrays within structs, but not generally? 5 answers One can assign a struct to another, which results in copying all the values from struct to another: struct { int a, b, c; } a, b; ... a = b; But why are arrays not assignable like that: int a[3], b[3]; ... a = b; Because, strictly speaking, are structs just arrays with variable sized elements, so why is that not allowed? This kind of assignment is

C++ Object Instantiation vs Assignment

混江龙づ霸主 提交于 2019-11-27 19:52:29
What is the difference between this: TestClass t; And this: TestClass t = TestClass(); I expected that the second might call the constructor twice and then operator=, but instead it calls the constructor exactly once, just like the first. TestClass t; calls the default constructor. TestClass t = TestClass(); is a copy initialization . It will call the default constructor for TestClass() and then the copy constructor (theoretically, copying is subject to copy elision ). No assignment takes place here . There's also the notion of direct initialization : TestClass t(TestClass()); If you want to

C++ Object Instantiation vs Assignment

旧城冷巷雨未停 提交于 2019-11-26 20:05:55
问题 What is the difference between this: TestClass t; And this: TestClass t = TestClass(); I expected that the second might call the constructor twice and then operator=, but instead it calls the constructor exactly once, just like the first. 回答1: TestClass t; calls the default constructor. TestClass t = TestClass(); is a copy initialization . It will call the default constructor for TestClass() and then the copy constructor (theoretically, copying is subject to copy elision ). No assignment