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
9
votes
1 answer

C++ OutputIterator post-increment requirements

C++ requires that an OutputIterator type X support expressions of the form r++, where r is an instance of X. This postfix increment must be semantically equivalent to: (*) { X tmp = r; ++r; return tmp; } and must return a type that is convertible…
nknight
  • 1,034
  • 8
  • 17
9
votes
3 answers

How does expression evaluation order differ between C++ and Java?

I've had my brain wrinkled from trying to understand the examples on this page: http://answers.yahoo.com/question/index?qid=20091103170907AAxXYG9 More specifically this code: int j = 4; cout << j++ << j << ++j << endl; gives an output: 566 Now this…
Chironex
  • 809
  • 3
  • 10
  • 28
8
votes
6 answers

Performance difference between using int a=a+1 and a++ in Java

Is there any performance difference between using int a=a+1 and a++ in Java? If so which is better and why? Could you briefly explain me to understand this?
Surya Chandra
  • 1,561
  • 5
  • 27
  • 42
8
votes
2 answers

Constructing std::pair of integers with a variable using post-increment

I tried constructing pairs of integers where the second integer is greater than the first by 1: 1 2 2 3 3 4 Using both std::make_pair and the constructor like so: std::make_pair(n, n++); However, this results in the pairs being in reverse: 2 1 3…
Pesho_T
  • 814
  • 1
  • 6
  • 18
8
votes
3 answers

why these both post increment in PHP gives the same answer?

I am trying to run the following code in PHP through localhost, but its giving the unexpected output! //answer is 3 but answer should be 2 due to post increment here is another code and it gives the…
RAK
  • 109
  • 7
8
votes
4 answers

Java post-increment (++) not behaving as expected when passed as a parameter

I came across the following issue: private void doStuff(int i) { if(i>10) { return; } doStuff(i++); } public void publicMethod() { doStuff(i); } I would expect this to run doStuff 10 times and then return. However i++ does not…
dngfng
  • 1,923
  • 17
  • 34
8
votes
4 answers

The assignment to variable has no effect?

When I do this: count = ++count; Why do i get the warning - The assignment to variable count has no effect ? This means that count is incremented and then assigned to itself or something else ? Is it the same as just ++count ? What happens in…
sweet dreams
  • 2,094
  • 13
  • 32
  • 46
7
votes
1 answer

Increment operator/ iterator implementation

I am trying to figure out a couple of things here: How do I write an increment operator for a node class that has a pointer to the next node? How do I implement iterators for a class like below? #include #include using namespace…
Chenna V
  • 10,185
  • 11
  • 77
  • 104
7
votes
4 answers

Overloaded 'operator++' must be a unary or binary operator (has 3 parameters)

I have a header file and a .cpp file. I am trying to implement a prefix and postfix operator overload but I keep getting this error when setting up the overload. fraction.h #ifndef FRACTION_H #define FRACTION_H #include using…
6
votes
3 answers

Is ++ the same as += 1 for pointers?

I'd like to refactor some old C code of mine, and I was curious if I can replace all ptr++ with ptr += 1 where ptris some pointer, without changing any behavior. Here's an example of what I mean, from K&R Section 5.3: /* strlen: return length of…
Mike Nichols
  • 119
  • 2
  • 10
6
votes
8 answers

Post-increment in a while loop

The following code confused me a bit: char * strcpy(char * p, const char * q) { while (*p++=*q++); //return } This is a stripped down implementation of strcpy function. From this code, we see that pointer p and q are incremented then…
Davita
  • 8,928
  • 14
  • 67
  • 119
6
votes
4 answers

Why ++str and str+1 is working and str++ isn't?

I know that here are some explanations about the difference between p++, ++p and p+1 but I couldn't understand it clearly yet, especially when it's not working with that function: void replace(char * str, char c1, char c2){ if (*str == '\0') { …
Joe
  • 109
  • 6
6
votes
1 answer

Difference between &++x and &x++

Although this question might be answered somewhere and I could not find it. Below written first statement work whereas second does not? WHY? int main() { int x = 1, y = 2; int *p = &++x; // First std::cout << "*p = " << *p << std::endl; //…
6
votes
2 answers

C++ post-increment: objects vs primitive types

We cannot use pre-increment on rvalues: int i = 0; int j = ++i++; // Compile error: lvalue required If we define a class: class A { public: A & operator++() { return *this; } A operator++(int) { A temp(*this); …
Null
  • 349
  • 2
  • 12
6
votes
4 answers

implementing a C++ postfix increment operator

I compiled the following example: #include #include using namespace std; class myiterator : public iterator { int* p; public: myiterator(int* x) :p(x) {} myiterator(const myiterator& mit) :…
chris
  • 3,986
  • 1
  • 26
  • 32