Questions tagged [compound-assignment]

For questions about single operators that assign a value based on both a previous value and an operand (e.g., the += operator in C or Python). Also referred to as "augmented assignment." Use this tag if your problem specifically involves or relates to a compound-assignment operator.

Compound assignment operators, such as +=, combine aspects of assignment with aspects of regular binary operators. For example, i += 42 generally has the effect of i = i + 42, but is more concise.

However, that is not always the case. For example, in a Python 3 class, a custom + can be implemented using the __add__ method, and a custom += can be implemented using the __iadd__ method. Those two methods could behave very differently.

102 questions
0
votes
2 answers

Compound assignment and add operator overloading

I need help with both of my operator overloading functions presented below. I'm unsure of how I can implement this without actually using the assignment in the function definitions. Code for operator + in my .cpp file: MyString& MyString::operator…
user1363061
  • 105
  • 1
  • 4
  • 12
-1
votes
1 answer

Problem in setting a variable defined in an enclosing scope

I have one map object named itemClassificationMap that gets its value from readClassificationData() method of ApachePOIExcelUtil class. Map itemClassificationMap = new…
Priyabrat
  • 21
  • 4
-1
votes
1 answer

Genetic algorithm for specific assignment

Assume I have a working place with many rooms. Every room needs employees of specific specialities. Every employee has his specialities. I need to assign employees to work in the rooms for a week and I want it to be done in the most efficient…
-1
votes
4 answers

what *p1 ^= *p2; does in c

In C what does this statement do? *p1 ^= *p2; p1 and p2 are char pointers pointing to two different address of a char array. I know about ^ operator is XOR.
Sarfaraz
  • 7
  • 1
-1
votes
1 answer

Array elements (after using compound assignment operator) yields garbage value, why?

#include void main(void) { int a[5]={90,78,77,98,98}, b[5]={80,45,67,88,57}, c[5]={88,99,65,55,74},total[3],i,j; for(j=0;j<=4;j++) { total[0]+=a[j]; total[1]+=b[j]; total[2]+=c[j]; …
이동준
  • 25
  • 1
  • 5
-1
votes
1 answer

How to overload compound-assignment in c++?

point.h ... Point operator +=(double & left, const Point & right) { return Point(left + right.x, left + right.y, left + right.z); } // std::ostream is overloaded ... main.cpp #include #include "point.h" int main() { double a =…
-1
votes
1 answer

Set Python attributes from a list, but recall them outside of a list

Not sure if this the recommended way of doing things in Python. I have a class with a bunch of attributes, like so class MyClass: def __init__(self): self.attr1 = "first_value" self.attr2 = "second_value" ... However,…
Samuel Tan
  • 1,700
  • 5
  • 22
  • 35
-2
votes
1 answer

Getting "no match for 'operator+=', no known conversion from argument 1 from 'int' to 'const ItemStack&'

I've been having a really hard time finding the correct way to overload the += operator. I used what seems to be the most popular method of doing it but it doesn't seem to meet the needs of this program. If anyone can help me solve this compiler…
Zivian
  • 1
  • 2
-2
votes
1 answer

Why isn't the compound assignment taken into account when displaying the latter 'tot'?

#include main() { int pro; int dot; int tot; char prelude[] = "\nNow we shall do some simple mathematics\n\n"; printf("%s", prelude); pro = 3; dot = 5; tot = pro + dot; …
-3
votes
3 answers

C# Compound Assignment (e.g. +=, -=, etc.) "Exceptions"

I'm reading a book on C#, and it has this to say about compound assignments (e.g. +=, -=, *=, /=, <<=, >>=): A subtle exception to this rule is with events, which we describe in Chapter 4: the += and -= operators here are treated specially and map…
TimFoolery
  • 1,895
  • 1
  • 19
  • 29
-4
votes
3 answers

Clarification on /= operator

In the following code #include using namespace std; int main(void){ double *x, *y; unsigned long long int n=2; x = new double [2]; y = new double [2]; for(int i=0; i<2; i++){ x[i] = 1.0; y[i] = 1.0; //what is…
James
  • 383
  • 1
  • 4
  • 15
-6
votes
3 answers

For loop confusion

I have : #include int main(void) { int s,i,t[] = { 0, 1, 2, 3, 4, 5 }; s = 1; for(i = 2; i < 6 ; i += i + 1) s += t[i]; printf("%d",s); return 0; } Why is the result 8? What I'm thinking: first loop: 2<6 True i+=i+1 is 5 s=1+t[5]…
Ady Alex
  • 29
  • 5
1 2 3 4 5 6
7