Multiple Assignment

closed account (NAXz3TCk)
I know that c++ allows for statements like

int a, b, c;

a = b = c = 10;

but is there a way that I can turn

a += 5;
b += 5;
c += 5;

into one line? Such as a,b,c += 5;

Thanks!
Never assigned variables this way, but have you tried
a = b = c += 5
closed account (NAXz3TCk)
No, that assigns a, b, and c to c+5.
try parenthesizing the "c+=5"
frice2014
No, that assigns a, b, and c to c+5.


I did it that way because you were adding the same values to them(5) and they all started with the same values(10)
Last edited on
closed account (NAXz3TCk)
Does anyone know if it is possible to do something like

int a = 1;
int b = 2;
int c = 3;

a, b, c += 3;

to achieve

a = a + 3;
b = b + 3;
c = c + 3;
That is starting to look more like a recursive operation than variable assignment.
Why not just use an array then use a for each loop to add five to each.
Three lines might be the best thing you can get for this...still thinking of your solution though.
Got it. Its kinda werid...but tell me if you find it satisfactory.
(a += 3) + (b += 3) + (c += 3);
closed account (o1vk4iN6)
Why not just do this instead of adding redundant code:

 
a += 3; b += 3; c += 3;
I don't know of a way to do what you're trying to do, but I'm honestly not sure why you'd bother. What's the incentive to try and cram multiple operations onto the same line? You're much better off making each operation clear and distinct. That will make your code easier to read, easier to understand, and easier to maintain.
Xerzi's way is better. Mines is a sloppy version to do that. Pretty sure it doesn't get easier than that but if anyone finds a better solution i'll be curious to see it.
Xerzi's way is better. Mines is a sloppy version to do that. Pretty sure it doesn't get easier than that but if anyone finds a better solution i'll be curious to see it.

Packing multiple operations and/or statements into the same line is straightforward - both you and Xerzi have come up with ways.

I think what the OP is looking for is something subtly different - a syntax construct to apply the same operation to multiple variables, concisely in one statement. I don't know of any such syntax in standard C++.

If you want to apply the same operation repeatedly, writing a function to do it might be useful, especially if the operation has some meaning that can be helpfully expressed in a function name.

But, as I said above, I'm not a fan of trying to pack multiple operations into as small a space as possible just for the sake of saving space. It tends to lead to code that is harder to read, harder to understand, and therefore harder to maintain.

Last edited on
How about making a function using elipses?
It's been a month since I read about elipses and I haven't practiced much with them since, but they let you create a function that can take an infinite number of variables. I don't remember the exact setup for it, but basically in defining the function you make the last variable an elipse (...) and then inside the function you decide what you're going to do with any variables passed to the function.

Hmm. this is the only example of an elipse function that I have in my notes and all it does is add the variable passed to the function together and returns the sum. It's also looks like it's in CLR/CLI language, so I don't know if native C++ has a similar option. If you don't know CLR/CLI then ignore this, I decided to ignore CLR/CLI until I understand C++ better anyways;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int elipses(int count, ...)//an example of an elipses used to allow for infinite number of variables into a function.
{
	if(count<=0) //count is the number of variables that you are passing to the function
	{
		return 0;
	}
	va_list arg_ptr; //all this va_list and arg_ptr is how you access the list of variables
	va_start(arg_ptr, count);
	int sum=0;
	for(int i=0; i<count; i++)
		sum += va_arg(arg_ptr, int);//this is where everything in the list is added to a single sum
	va_end(arg_ptr);
	return sum;
}


EDIT;
Never mind, I just tried experimenting with it and I couldn't figure out a way to pass the values by reference, which is what you would need to do in order to get it to work the way I was talking about. You might be able to find a tutorial online that could help you with that so I'll leave this up, but it's looking like a dead end.
Last edited on
That's a possibility. And if you know that all of the variables you want to perform the operation on are int, you could have the method take a vector of ints, instead of using ellipses.
Topic archived. No new replies allowed.