Ye Olde General C++ Discussion/Advice Thread

What would you say is the value of j? If you are sure of your answer, run the code. :)

Well, I guess 12 might make some logical sense. The first part (k++) returns 5, while k is incremented to 6. The next part (++k) then increments k (6) before returning its value, meaning that 7 is returned. 5+7 =12. Is that what happened?
 
Well, I guess 12 might make some logical sense. The first part (k++) returns 5, while k is incremented to 6. The next part (++k) then increments k (6) before returning its value, meaning that 7 is returned. 5+7 =12. Is that what happened?

The ++k increments k to 6 before the j = ... statment is run, so it actually does 6+6=12. After this k is incremented to 7, but it is never used again.
 
The ++k increments k to 6 before the j = ... statment is run, so it actually does 6+6=12. After this k is incremented to 7, but it is never used again.
Hmm, which means that there's no functional difference between ++k and k++ when they're used together, inline in that fashion. But I do know that the side of the ++ indicator is meant to indicate whether the variable is incremented before or after its value is returned ... just can't think now of a case where that'd result in a difference (if it doesn't here).
 
Some fun with obfuscation: Without compiling, what does this output, and why does/doesn't it work?

Code:
#include <iostream>

int main() {
#include <iostream>

int main() {
  std::cout << &(6 + ((long)"Well, hello there." & 0x3))["Hello World"] << std::endl;  
}
 
Last edited:
Hmm, which means that there's no functional difference between ++k and k++ when they're used together, inline in that fashion. But I do know that the side of the ++ indicator is meant to indicate whether the variable is incremented before or after its value is returned ... just can't think now of a case where that'd result in a difference (if it doesn't here).

The ++ prefix just means before the statement, the ++ suffixmeans after the statement. It's not really that they're used together, but rather that k++ will just never have an effect on the current statement.
 
The ++ prefix just means before the statement, the ++ suffixmeans after the statement. It's not really that they're used together, but rather that k++ will just never have an effect on the current statement.

Ok, let me word my point as a question rather: in what circumstance would using k++ return a different result to ++k; or are they completely interchangeable with no functional difference?
 
Ok, let me word my point as a question rather: in what circumstance would using k++ return a different result to ++k; or are they completely interchangeable with no functional difference?

They're not interchangeable at all: ++k means before the statment and k++ is incremented after the statment. It's not done in place, so they have very different meanings.

Extending the example may make it much clearer.
Code:
  k = 5;
  std::cout << k++ + k++ + k++ + k++ << std::endl;
  k = 5;
  std::cout << ++k + ++k + ++k + ++k << std::endl;

Outputs: 20 (5 + 5 + 5 + 5) and 36 (9 + 9 + 9 + 9)
If it was incrementing in place (rather than before or after the entire statement), it would give 5 + 6 + 7 + 8 = 26, and 6 + 7 + 8 + 9 = 30.
 
They're not interchangeable at all: ++k means before the statment and k++ is incremented after the statment. It's not done in place, so they have very different meanings.

Extending the example may make it much clearer.
Code:
  k = 5;
  std::cout << k++ + k++ + k++ + k++ << std::endl;
  k = 5;
  std::cout << ++k + ++k + ++k + ++k << std::endl;

Outputs: 20 (5 + 5 + 5 + 5) and 36 (9 + 9 + 9 + 9)
If it was incrementing in place (rather than before or after the entire statement), it would give 5 + 6 + 7 + 8 = 26, and 6 + 7 + 8 + 9 = 30.

Oooooooh, now I see. That's fascinating, I didn't know that '++k' would end up adding up all the times that '++k' is used and then apply them cumulatively to each and every value returned!
 
The ++k increments k to 6 before the j = ... statement is run, so it actually does 6+6=12. After this k is incremented to 7, but it is never used again.

You are correct, but only since you use a certain compiler. The C++ Standard does not presuppose the calculation of the ++ operator before the assignment operator is done. This basically means that the answer may be different depending on the compiler you use. Most of us would get j as 12. :)

More info: http://en.wikipedia.org/wiki/Sequence_point
 
You are correct, but only since you use a certain compiler. The C++ Standard does not presuppose the calculation of the ++ operator before the assignment operator is done. This basically means that the answer may be different depending on the compiler you use. Most of us would get j as 12. :)

More info: http://en.wikipedia.org/wiki/Sequence_point

Great point - and for emphasis, I want to point out that this actually does generate different results on different compilers, it's not just a "standard didn't cover this, but X always makes sense". In general k++ (when called multiple times between seq points) and ++k (when called multiple times between seq points) will still likely give different results, but whether or not they're all pre/post-incremented before the statement, or pre/post-incremented before each add is ambiguous (and conceivably, it could just do something completely unintuitive).
 
Last edited:
I remember reading that ++i is generally advocated over i++ for speed reasons, since with i++, it returns a result that you aren't using (something like that).
 
Inferred return type / polymorphism / overloading

Why is the following C++ function implementation correct/incorrect and why would you be mistaken for thinking it is correct/incorrect?

addone(x) {
var result; /* inferred-type variable result */
var result2; /* inferred-type variable result #2 */

result = x + 1;
result2 = x + 1.0; /* this line won't work (in the proposed language) */
return result;
}

spoiler and the fascinating world of polymorphism which is commonly available with function overloading.
 
I've never seen the var keyword in C++ before. Even in languages that support it, such as C#, var result will result in a compiler error since it can't figure out the type at compile time. var result will only work in languages such as Javascript.
 
I've never seen the var keyword in C++ before. Even in languages that support it, such as C#, var result will result in a compiler error since it can't figure out the type at compile time. var result will only work in languages such as Javascript.

You can alias any variable type you like in c++. In this case the Wikipedia don't make this very clear. Just because you are using standard Windows SDK type definition like wchar, doesn't mean these are standard C types.
 
I remember reading that ++i is generally advocated over i++ for speed reasons, since with i++, it returns a result that you aren't using (something like that).
No difference in speed. Only a poorly written compiler will return a result if you don't need one.
 
You can alias any variable type you like in c++. In this case the Wikipedia don't make this very clear. Just because you are using standard Windows SDK type definition like wchar, doesn't mean these are standard C types.

I agree with Ancalagon on this - I'm not sure what you are trying to ask, since that code snippet is not C++, and doesn't have a meaningful analog in C++. The wikipedia article states: "In a hypothetical language supporting type inference, the code might be written like this instead:" prior to listing the code.
 
I agree with Ancalagon on this - I'm not sure what you are trying to ask, since that code snippet is not C++, and doesn't have a meaningful analog in C++. The wikipedia article states: "In a hypothetical language supporting type inference, the code might be written like this instead:" prior to listing the code.

Haha just shows what I know about c++
 
Top
Sign up to the MyBroadband newsletter
X