assignment-operator

“Almost default” copy constructor (& assignment operator) in C++

岁酱吖の 提交于 2019-12-03 12:37:14
A common thing I find myself doing is making "almost default" copy constructors and assignment operators. That is, I find myself in situations where the compiler supplied copy and assignment operators would work for most of the data members, but there's a particular data member which needs to be handled differently. This means that I have to explicitly create a copy constructor/assignment operator, including explicitly listing all the data members which have simple copy semantics. This can get annoying for classes where there are a fair number of data members, or later on when member variables

Why is it not efficient to use a single assignment operator handling both copy and move assignment?

て烟熏妆下的殇ゞ 提交于 2019-12-03 07:54:48
问题 Here is an exercise from C++ Primer 5th Edition : Exercise 13.53: As a matter of low-level efficiency, the HasPtr assignment operator is not ideal. Explain why. Implement a copy-assignment and move-assignment operator for HasPtr and compare the operations executed in your new move-assignment operator versus the copy-and-swap version.(P.544) File hasptr.h : //! a class holding a std::string* class HasPtr { friend void swap(HasPtr&, HasPtr&); friend bool operator <(const HasPtr& lhs, const

const member and assignment operator. How to avoid the undefined behavior?

百般思念 提交于 2019-12-03 05:19:28
I answered the question about std::vector of objects and const-correctness , and received a comment about undefined behavior. I do not agree and therefore I have a question. Consider the class with const member: class A { public: const int c; // must not be modified! A(int c) : c(c) {} A(const A& copy) : c(copy.c) { } // No assignment operator }; I want to have an assignment operator but I do not want to use const_cast like in the following code from one of the answers: A& operator=(const A& assign) { *const_cast<int*> (&c)= assign.c; // very very bad, IMHO, it is undefined behavior return

Python: yield and yield assignment

时光怂恿深爱的人放手 提交于 2019-12-02 22:33:54
How does this code, involving assignment and the yield operator, work? The results are rather confounding. def test1(x): for i in x: _ = yield i yield _ def test2(x): for i in x: _ = yield i r1 = test1([1,2,3]) r2 = test2([1,2,3]) print list(r1) print list(r2) Output: [1, None, 2, None, 3, None] [1, 2, 3] The assignment syntax ("yield expression") allows you to treat the generator as a rudimentary coroutine. First proposed in PEP 342 and documented here: https://docs.python.org/2/reference/expressions.html#yield-expressions The client code that is working with the generator can communicate

Why is it not efficient to use a single assignment operator handling both copy and move assignment?

血红的双手。 提交于 2019-12-02 21:19:52
Here is an exercise from C++ Primer 5th Edition : Exercise 13.53: As a matter of low-level efficiency, the HasPtr assignment operator is not ideal. Explain why. Implement a copy-assignment and move-assignment operator for HasPtr and compare the operations executed in your new move-assignment operator versus the copy-and-swap version.(P.544) File hasptr.h : //! a class holding a std::string* class HasPtr { friend void swap(HasPtr&, HasPtr&); friend bool operator <(const HasPtr& lhs, const HasPtr& rhs); public: //! default constructor. HasPtr(const std::string &s = std::string()): ps(new std:

assignment operator String object

时光总嘲笑我的痴心妄想 提交于 2019-12-02 19:17:22
问题 I am new to JAVA programming. I have read it in my book String a="Hello"; String b="Hello"; System.out.println(a==b); This should return false as a & b refer to different instances of String objects. Bcoz the assignments operator compares the instances of objects but Still I am getting a true . I am using Eclipse IDE. Example in book goes as this: String s = "s"; String sToo = "s"; System.out.println(a == b); System.out.println(s == sToo); That bit of code prints “false” for s == sToo. That's

How to implement an assignment operator in linked list class

假如想象 提交于 2019-12-02 18:52:15
问题 I am having difficulty figuring out how to implement the rule of 5 in my doubly linked list class. I get the concept of them, it's just lost with how to code it. I have attempted the destructor and copy operator, but at a stand still going forward with the rest. Any help/guidance is appreciated, thanks. destructor/copy: ~DList() { Node* current = front_; while (current != back_) { front_ = front_->next_; delete current; current = front_; } } // copy ctor DList(const DList& rhs) { const Node*

Shortcut “or-assignment” (|=) operator in Java

▼魔方 西西 提交于 2019-12-02 10:46:53
I have a long set of comparisons to do in Java, and I'd like to know if one or more of them come out as true. The string of comparisons was long and difficult to read, so I broke it up for readability, and automatically went to use a shortcut operator |= rather than negativeValue = negativeValue || boolean . boolean negativeValue = false; negativeValue |= (defaultStock < 0); negativeValue |= (defaultWholesale < 0); negativeValue |= (defaultRetail < 0); negativeValue |= (defaultDelivery < 0); I expect negativeValue to be true if any of the default<something> values are negative. Is this valid?

How can the assignment from int to object be possible in C++?

蓝咒 提交于 2019-12-02 08:57:01
class phone { public: phone(int x) { num = x; } int number(void) { return num; } void number(int x) { num = x; } private: int num; }; int main(void) { phone p1(10); p1 = 20; // here! return 0; } Hi, guys Just I declared a simple class like above one. After that I assigned int value to the object that class, then it worked! (I printed its value. It was stored properly) If there is not a construct with int parameter, a compile error occurred. So, I think it's related with a constructor. Is that right? Please give me a good explanation. Thanks. This is legal because C++ interprets any constructor

Shorthand assignment operator for inverting boolean

岁酱吖の 提交于 2019-12-02 08:15:33
There are shorthand operators for the basic arithmetic operators, such as: x = x+2; x += 2; or y = y*2; y *= 2; However, I was wondering if there was any such operator that could simply invert the value of a boolean. For example, assuming z = true , is there any shorter equivalent to: z = !z; I know it can't be just !z , because then it would just return the opposite value of z , but it wouldn't change its value. I know I'm kind of lazy, but I use this a lot in my code and am trying to optimize it as much as possible. I'd try to avoid repeating variable names as much as possible to keep it