assignment-operator

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

Does it make sense to use the move-and-swap idiom on a movable and non-copyable class

落花浮王杯 提交于 2020-05-12 21:20:05
问题 If I have a class such as class Foo{ public: Foo(){...} Foo(Foo && rhs){...} operator=(Foo rhs){ swap(*this, rhs);} void swap(Foo &rhs); private: Foo(const Foo&); // snip: swap code }; void swap(Foo& lhs, Foo& rhs); Does it make sense to implement operator= by value and swap if I don't have a copy constructor? It should prevent copying my objects of class Foo but allow moves. This class is not copyable so I shouldn't be able to copy construct or copy assign it. Edit I've tested my code with

Why does setting an element of a map to its size increments the size *before* assigning it?

送分小仙女□ 提交于 2020-05-11 04:52:41
问题 This is a common pattern I use to index tokens as they come in: check if the token is in a map, and if not, add it to the map, assigning the map's size. When doing this in C++, it unexpectedly increments the map's size before the assignment is made: #include <cstdio> #include <map> using namespace std; int main() { map<char, int> m; printf("Size before adding: %d\n", m.size()); m['A'] = m.size(); printf("Size after adding: %d\n", m.size()); printf("What was added: %d\n", m['A']); return 0; }

Why does setting an element of a map to its size increments the size *before* assigning it?

霸气de小男生 提交于 2020-05-11 04:52:24
问题 This is a common pattern I use to index tokens as they come in: check if the token is in a map, and if not, add it to the map, assigning the map's size. When doing this in C++, it unexpectedly increments the map's size before the assignment is made: #include <cstdio> #include <map> using namespace std; int main() { map<char, int> m; printf("Size before adding: %d\n", m.size()); m['A'] = m.size(); printf("Size after adding: %d\n", m.size()); printf("What was added: %d\n", m['A']); return 0; }

Why does setting an element of a map to its size increments the size *before* assigning it?

落爺英雄遲暮 提交于 2020-05-11 04:51:59
问题 This is a common pattern I use to index tokens as they come in: check if the token is in a map, and if not, add it to the map, assigning the map's size. When doing this in C++, it unexpectedly increments the map's size before the assignment is made: #include <cstdio> #include <map> using namespace std; int main() { map<char, int> m; printf("Size before adding: %d\n", m.size()); m['A'] = m.size(); printf("Size after adding: %d\n", m.size()); printf("What was added: %d\n", m['A']); return 0; }