I suspect the answer is no, but is there any equivalent to the C++ unary pre and postfix incremement operator "++". For example.
int test = 1;
SomeFunc(test++); // test is 1 inside SomeFunc and 2 afterwards
test = 1;
Somefunc(++test); // test is 2 inside SomeFunc and 2 afterwards
I know about the Inc (and Dec) operator in Delphi, but you can't pass it to a function as in:
test: Integer;
//...
SomeFunc(Inc(test)); // compiler error, incompatible types
In addition to the compilation error, there does not appear to be a different pre and postfix increment. Its not a big problem writing code like this:
SomeFunc(test);
test := (test + 1);
SomeFunc(test);
but the ++ (and --) operators in C++ are a great feature.
In mathematics, an unary operation is an operation with only one operand, i.e. a single input. This is in contrast to binary operations, which use two operands. An example is any function f : A → A, where A is a set.
A unary operator is an operator used to operate on a single operand to return a new value. In other words, it is an operator that updates the value of an operand or expression's value by using the appropriate unary operators.
Unary operators: are operators that act upon a single operand to produce a new value. Types of unary operators: unary minus(-) increment(++) decrement(- -)
This operator is represented by the symbol, "T," where T is the type to which the operand or the result of the expression must be converted.
There is no equivalent functionality built in to Delphi.
You might contemplate writing functions like this:
function PostInc(var Value: Integer): Integer;
begin
Result := Value;
inc(Value);
end;
function PreInc(var Value: Integer): Integer;
begin
inc(Value);
Result := Value;
end;
You would probably wish to make any such functions inline. Although it is open to debate as to how useful such functions would be.
Personally I feel that these operators are sometimes convenient in C and C++, but the case for them is not overwhelming. Certainly for beginners, they present a huge trap to fall in to as can be seen by the steady stream of questions here asking about expressions like ++i++ + i++
.
FWIW, your description of the operators is imprecise. You said:
int test = 1;
SomeFunc(test++); // test is 1 inside SomeFunc and 2 afterwards
That is not correct. The variable test
is incremented before SomeFunc
is called, because a function call is a sequence point. So, test
has value 2
if observed from inside SomeFunc
. But the value passed to SomeFunc
is 1
. This program:
#include <iostream>
int test = 1;
void foo(int x)
{
std::cout << x << std::endl;
std::cout << test << std::endl;
}
int main()
{
foo(test++);
}
outputs
1 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With