Problem with understanding Operators

Started by
16 comments, last by TDragon 18 years, 6 months ago
realize that the problem is only because the ++ operator is used on the SAME value twice between sequence points ... the following is 100% defined:

int a = 2;
int b = 3;

++a + ++b;

the above will equal 7, as guaranteed by the standard.
Advertisement
Of course it doesn't guarantee if a is incremented first or b is incremented first (as might matter if they were of class type rather than int type).
Something that hasn't been mentioned thus far is that multiple increment operators within expressions is bad programming practice -- even obfuscation to some people. Your code is usually more easily read when you increment in a separate statement.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
Mmm. There are well know idioms that involve incrementing multiple things in the same statement. ex:

while (*src++ = *dest++);

Though this is more of a C-ism, it's a standard C-ism; a competent C programmer should be able to recognize it.
Eh, first time I've ever seen anything like that. Mind mentioning the source?

It might take a while, especially for a beginner, to figure out what that actually does...copies from one portion of memory (a null terminated string?) to another until it reaches a null/0 slot, is how I interpreted it...
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
It's an inline strcpy() from before inline functions. You see it all the time in old C code.
It copies a null terminated list. As far as source it's so basic and common it would be virtually impossible to say who first did it.
Keys to success: Ability, ambition and opportunity.
Basic and common, but pointless in C++ and apparently deprecated in C.

In any case, I'm sure we all agree that for someone who's learning C++, simplicity and/or transparency are imperative.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++

This topic is closed to new replies.

Advertisement