Questions tagged [post-increment]

For issues relating to defining or performing post increment operations.

Post-increment operators increase the value of their operand by 1 (or another specified amount), but the value of the operand in the expression is the operand's original value prior to the increment operation.

561 questions
4
votes
1 answer

Overloaded postincrement operator squares value of complex number without instantiation of operator

Here is the output: First Complex Number: Enter real part of complex number: 3 Enter imaginary part of complex number: 6 Second Complex Number: Enter real part of complex number: 5 Enter imaginary part of complex number: -5 a == (-27.00+36.00i) b…
garyoak
  • 41
  • 3
4
votes
6 answers

what does ++ exactly mean in Swift?

i am learning Swift with a book aimed for people with little experience. One thing bothering me is the ++ syntax. The following is taken from the book: var counter = 0 let incrementCounter = { …
Tang1230
  • 65
  • 1
  • 6
4
votes
3 answers

invalid initialization of non-const reference of type 'int&', what the reason?

I have the given code, which gets an error: error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int' const int b = f(a++); ^ int f(int& a) { return a; } int main() { //…
user1886376
4
votes
4 answers

x = x++ doesn't increment because the ++ is applied after the assignment?

From page 280 of OCP Java SE 6 Programmer Practice Exams, question 9: int x = 3; x = x++; // x is still 3 In the explanation we can read that: The x = x++; line doesn't leave x == 4 because the ++ is applied after the assignment has occurred. I…
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
4
votes
3 answers

C# allow public incrementation of property, but not explicit seting

What I want to do is allow the public incrementation of an integer value within my class, but not allow it to be publicly set explicitly. I know that I can create a function like this: void IncrementMyProperty() but I'd like to allow the user to…
OneHoopyFrood
  • 3,829
  • 3
  • 24
  • 39
4
votes
1 answer

How are java increment statements evaluated in complex expressions

What is the output of the following code: int x = 2; x += x++ * x++ * x++; System.out.println(x); I understand that ++variableName is pre-increment operator and the value of variableName is incremented before it is used in the expression…
Piyush Saravagi
  • 369
  • 2
  • 10
4
votes
6 answers

post increment with addition in Java

I have following code in java int x=5; System.out.println(x++ + ++x); The output is 12. I thought it should be 11. We have three operators here: + addition ++ (post) ++ (pre) List item In which order does the above print statement compile? If I…
user1765876
4
votes
3 answers

Difference in Increment-decrement operator in C and JAVA

Please consider the following statement: int a[]={1,2,3,4,5,6,7,8}; int i=0,n; n=a[++i] + i++ + a[i++] + a[i] ; According to my logic n should be 10. But I am getting different output in c (output is 7) However in java I am getting expected result…
Abhas Tandon
  • 1,859
  • 16
  • 26
4
votes
3 answers

Post-incrementing an iterator after de-referencing - *iter++. How is it evaluated?

I couldn't find a better title for this issue, please update it if needed as per the below question. Consider an iterator - iter, pointing to elements in a std::vector. I'm inserting the value of *iter at the current iterator position…
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
4
votes
2 answers

Post-increment operator in C

I was casually coding when I wrote this C code: #include int main() { int i; i = 10; printf("i : %d\n",i); printf("sizeof(i++) is: %d\n",sizeof(i++)); printf("i : %d\n",i); return 0; } And when I ran the code, the…
user2290802
  • 253
  • 2
  • 7
4
votes
1 answer

Why is there no difference in ++foo and foo++ when the ++ operator is overloaded?

Possible Duplicate: Post-increment Operator Overloading Why are Postfix ++/— categorized as primary Operators in C#? I saw that I can overload the ++ and -- operators. Usually you use these operators by 2 ways. Pre and post increment/deccrement…
Bosak
  • 2,103
  • 4
  • 24
  • 43
3
votes
2 answers

Sybase add incrementing counter to a select statement

It seems that a similar question has been asked an solutions exist for other DB products (especially MS-SQL) but they don't work for sybase so I'm asking this question. I have a simple select statement and I'd like to get a column containing and…
Victor Parmar
  • 5,719
  • 6
  • 33
  • 36
3
votes
2 answers

C++ post-increment operator overload in iterators (compiling with -Wall -Werror)

I'm currently creating my own iterator for a b-tree, and I'm stuck on how to implement the post-increment operator without the compiler complaining. The error message is as follows, and is expected (since I am doing exactly what the error message…
Mick
  • 665
  • 2
  • 6
  • 18
3
votes
0 answers

The below two C codes gives two different outputs, can someone explain why?

The first code is : #include int main() { int a,b,c; a = 4; b = ++a; c = b + a++; printf("%d",c); return 0; } Output is 10 The second code is : #include int main() { int a,b; a = 4; b = ++a + a++; …
Harri
  • 39
  • 3
3
votes
2 answers

Make ++o++ complain for types with user defined pre- and postfix increment operators

I'm looking for a way to prevent ++x++ from working for types with user defined prefix and postfix increment operators. For builtin types the result type of the postfix operator is not an lvalue but a prvalue expression and the compilers complain…
jesses
  • 559
  • 3
  • 15