If I have for example a class with instance method and variables
class Foo
{
...
int x;
int bar() { return x++; }
};
Is the behavior of returning a post-incremented variable defined?
If I have for example a class with instance method and variables
class Foo
{
...
int x;
int bar() { return x++; }
};
Is the behavior of returning a post-incremented variable defined?
Yes, it's equivalent to:
int bar()
{
int temp = x;
++x;
return temp;
}
++x
to x++
; just to be correct. –
Achromatous x++
to a ++x
otherwise I would be defining x++
in terms of x++
, and I tend to try to avoid circular definitions. Also, I don't believe it is a mistake, in the same way that I didn't make the clarification that my code only applies if there isn't a #define for temp or bar... Furthermore, my example only describes the operation for an int
where it is 100% correct. –
Bosk Yes it is ... it will return the x's value before incrementing it and after that the value of x will be + 1 ... if it matters.
It is defined.
It returns the value of x
before incrementation. If x
is a local(non-static) variable this post incrementation has no effect since local variables of a function cease to exist once the function returns. But if x
is a local static variable, global variable or an instance variable( as in your case), its value will be incremented after return.
++
-operator on a custom class? Will it's effect be performed? –
Gravimeter return x.dosomething();
, the effect of dosomething
will be performed before the return. Overloaded post-increment isn't magic, it's just a function that returns a value, that happens to be the old value. –
Queasy I know this question is answered long before but here's why it is defined. Compound operators are basically syntax sugar for functions. If you're wondering how the increment happens after returning from the function, it doesn't. It happens just before the operator "function" returns the previous value.
For an integer, think of the post increment operator function defined like this:
int post_increment(int *n)
{
int temp = *n;
*n = *n + 1;
return temp;
}
Most programming languages, like C++, are recursive in the order that operations are carried out (I'm not making any implication about how the code is actually implemented by the compiler here). Compound operations that are composed of any well defined operations are themselves well defined, since each operation is carried out on a last-in, first-out basis.
Post-increment returns the value of the variable being incremented before incrementing it, so the return
operation recieves that value. No special definition of this behavior has to be made.
return
statement is encountered after x++
"returns". Since x++
is an operation, it should be thought of as returning as in the demonstration code that Poita_ provided. That way, it's easy to see that since x++
is executed and its return value is passed to the return
statement. It doesn't make sense that x++
would return and then increment. –
Hillery I think it is defined but not preferred. It causes confusion to people. For example, the following code prints 1 instead of 2.
#include <iostream>
#include <cstdlib>
using namespace std;
int foo()
{
int i = 1;
return i++;
}
int main()
{
cout << foo() << endl;
return 0;
}
using namespace std
. That prefix is there for a reason: To protect you from naming collisions and to make it clear where those functions, data structures and other things come from. –
Holding © 2022 - 2024 — McMap. All rights reserved.
int
is not anint
, but aMyClassWithOverloadedOperators
, then you're just wrong. – Achromatous