Questions tagged [assignment-operator]

The operator used when one object is assigned to another

In C++ an assignment operator is implicitly-declared for all class types and can optionally be overloaded to provide a custom implementation or to define assignment from different types.

In C++11 the traditional assignment operator is known as the copy-assignment operator, to distinguish it from the move-assignment operator.

1185 questions
3728
votes
11 answers

Why don't Java's +=, -=, *=, /= compound assignment operators require casting?

Until today, I thought that for example: i += j; Was just a shortcut for: i = i + j; But if we try this: int i = 5; long j = 8; Then i = i + j; will not compile but i += j; will compile fine. Does it mean that in fact i += j; is a shortcut for…
2284
votes
8 answers

What is The Rule of Three?

What does copying an object mean? What are the copy constructor and the copy assignment operator? When do I need to declare them myself? How can I prevent my objects from being copied?
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
2138
votes
5 answers

What is the copy-and-swap idiom?

What is this idiom and when should it be used? Which problems does it solve? Does the idiom change when C++11 is used? Although it's been mentioned in many places, we didn't have any singular "what is it" question and answer, so here it is. Here is…
GManNickG
  • 459,504
  • 50
  • 465
  • 534
805
votes
7 answers

What are the differences between "=" and "<-" assignment operators in R?

What are the differences between the assignment operators = and <- in R? I know that operators are slightly different, as this example shows x <- y <- 5 x = y = 5 x = y <- 5 x <- y = 5 # Error in (x <- y) = 5 : could not find function "<-<-" But…
csgillespie
  • 54,386
  • 13
  • 138
  • 175
210
votes
2 answers

Understanding exactly when a data.table is a reference to (vs a copy of) another data.table

I'm having a little trouble understanding the pass-by-reference properties of data.table. Some operations seem to 'break' the reference, and I'd like to understand exactly what's happening. On creating a data.table from another data.table (via <-,…
Peter Fine
  • 2,713
  • 3
  • 12
  • 15
115
votes
2 answers

What's the difference between `=` and `<-` in R?

I'm using R 2.8.1 and it is possible to use both = and <- as variable assignment operators. What's the difference between them? Which one should I use?
Mehper C. Palavuzlar
  • 8,999
  • 21
  • 54
  • 67
112
votes
5 answers

Reference assignment operator in PHP, =&

What does the =& (equals-ampersand) assignment operator do in PHP? Is it deprecated?
Kyle J. Dye
  • 1,586
  • 2
  • 9
  • 12
97
votes
5 answers

How to use base class's constructors and assignment operator in C++?

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…
Igor Oks
  • 24,930
  • 25
  • 85
  • 112
92
votes
8 answers

Shortcut "or-assignment" (|=) operator in Java

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…
David Mason
  • 2,674
  • 3
  • 28
  • 41
84
votes
11 answers

Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)

So for binary operators on booleans, Java has &, |, ^, && and ||. Let's summarize what they do briefly here: JLS 15.22.2 Boolean Logical Operators &, ^, and | JLS 15.23 Conditional-And Operator && JLS 15.24 Conditional-Or Operator || For &, the…
84
votes
8 answers

What is the motivation for Scala assignment evaluating to Unit rather than the value assigned?

What is the motivation for Scala assignment evaluating to Unit rather than the value assigned? A common pattern in I/O programming is to do things like this: while ((bytesRead = in.read(buffer)) != -1) { ... But this is not possible in Scala…
Graham Lea
  • 4,458
  • 3
  • 32
  • 40
84
votes
11 answers

Is it possible to overload Python assignment?

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 =…
Caruccio
  • 2,679
  • 3
  • 16
  • 12
76
votes
9 answers

Java assignment operator execution

In Java, I understand that assignment evaluates to the value of the right operand, so statements like x == (y = x) evaluate to true. This code, however, outputs false. public static void main(String[]args){ String x = "hello"; String y =…
Sam
  • 1,532
  • 2
  • 11
  • 26
73
votes
6 answers

Why must the copy assignment operator return a reference/const reference?

In C++, the concept of returning reference from the copy assignment operator is unclear to me. Why can't the copy assignment operator return a copy of the new object? In addition, if I have class A, and the following: A a1(param); A a2 = a1; A…
bks
  • 1,696
  • 1
  • 21
  • 43
67
votes
2 answers

Why are there no ||= or &&= operators in C#?

We have equivalent assignment operators for all Logical operators, Shift operators, Additive operators and all Multiplicative operators. Why did the logical operators get left out? Is there a good technical reason why it is hard?
1
2 3
78 79