assignment-operator

What does an ampersand after this assignment operator mean?

*爱你&永不变心* 提交于 2019-11-26 19:06:44
问题 I was reading through this nice answer regarding the "Rule-of-five" and I've noticed something that I don't recall seeing before: class C { ... C& operator=(const C&) & = default; C& operator=(C&&) & = default; ... }; What is the purpose of the & character placed in front of = default for the copy assignment operator and for the move assignment operator? Does anyone have a reference for this? 回答1: It's part of a feature allowing C++11 non-static member functions to differentiate between

Return value of assignment operation in Java

故事扮演 提交于 2019-11-26 16:42:58
I encountered a statement in Java while ((line = reader.readLine()) != null) { out.append(line); } How do assignment operations return a value in Java? The statement we are checking is line = reader.readLine() and we compare it with null . Since readLine will return a string, how exactly are we checking for null ? The assignment operator in Java returns the assigned value (like it does, e.g., in c ). So here, readLine() will be executed, and it's return value stored in line . That value is then checked against null , and if it is null , the loop will terminate. Assignment expressions are

Assign multiple objects to .GlobalEnv from within a function

回眸只為那壹抹淺笑 提交于 2019-11-26 16:34:30
A post on here a day back has me wondering how to assign values to multiple objects in the global environment from within a function. This is my attempt using lapply ( assign may be safer than <<- but I have never actually used it and am not familiar with it). #fake data set df <- data.frame( x.2=rnorm(25), y.2=rnorm(25), g=rep(factor(LETTERS[1:5]), 5) ) #split it into a list of data frames LIST <- split(df, df$g) #pre-allot 5 objects in R with class data.frame() V <- W <- X <- Y <- Z <- data.frame() #attempt to assign the data frames in the LIST to the objects just created lapply(seq_along

Reference assignment operator in PHP, =&

寵の児 提交于 2019-11-26 15:22:21
What does the =& (equals-ampersand) assignment operator do in PHP? Is it deprecated? It's not deprecated and is unlikely to be. It's the standard way to, for example, make part of one array or object mirror changes made to another, instead of copying the existing data. It's called assignment by reference , which, to quote the manual, "means that both variables end up pointing at the same data, and nothing is copied anywhere" . The only thing that is deprecated with =& is "assigning the result of new by reference" in PHP 5 , which might be the source of any confusion. new is automatically

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

守給你的承諾、 提交于 2019-11-26 14:23:16
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 to use base class&#39;s constructors and assignment operator in C++?

爱⌒轻易说出口 提交于 2019-11-26 12:50:59
I have a class B with a set of constructors and an assignment operator. Here it is: class B { public: B(); B(const string& s); B(const B& b) { (*this) = b; } B& operator=(const B & b); private: virtual void foo(); // and other private member variables and functions }; I want to create an inheriting class D that will just override the function foo() , and no other change is required. But, I want D to have the same set of constructors, including copy constructor and assignment operator as B : D(const D& d) { (*this) = d; } D& operator=(const D& d); Do I have to rewrite all of them in D , or is

C++ why the assignment operator should return a const ref in order to avoid (a=b)=c

戏子无情 提交于 2019-11-26 12:42:29
问题 I am reading a book about C++ and more precisely about the operator overloading. The example is the following: const Array &Array::operator=(const Array &right) { // check self-assignment // if not self- assignment do the copying return *this; //enables x=y=z } The explanation provided by the book about returning const ref instead of ref is to avoid assignments such as (x=y)=z. I don\'t understand why we should avoid this. I understand that x=y is evaluated first in this example and since it

Is it possible to overload Python assignment?

时间秒杀一切 提交于 2019-11-26 11:22:25
Is there a magic method that can overload the assignment operator, like __assign__(self, new_value) ? I'd like to forbid a re-bind for an instance: class Protect(): def __assign__(self, value): raise Exception("This is an ex-parrot") var = Protect() # once assigned... var = 1 # this should raise Exception() Is it possible? Is it insane? Should I be on medicine? The way you describe it is absolutely not possible. Assignment to a name is a fundamental feature of Python and no hooks have been provided to change its behavior. However, assignment to a member in a class instance can be controlled as

Overloading assignment operator in C++

我怕爱的太早我们不能终老 提交于 2019-11-26 11:07:12
问题 As I\'ve understand, when overloading operator=, the return value should should be a non-const reference. A& A::operator=( const A& ) { // check for self-assignment, do assignment return *this; } It is non-const to allow non-const member functions to be called in cases like: ( a = b ).f(); But why should it return a reference? In what instance will it give a problem if the return value is not declared a reference, let\'s say return by value? It\'s assumed that copy constructor is implemented

Value returned by the assignment

偶尔善良 提交于 2019-11-26 10:56:11
问题 Why does the regular assignment statement (say, x = 5 ) return the value assigned ( 5 in this case), while the assignment combined with a variable declaration ( var x = 5 ) returns undefined ? I got the return values by executing these statements in the Chrome browser\'s Javascript console: > var x = 5; undefined > y = 5; 5 回答1: That's the way the language was designed. It is consistent with most languages. Having a variable declaration return anything other than undefined is meaningless,