--Post-increment operator like a ++: Incremented after the value is passed to the function -Prefix increment operator like ++ b: Incremented before the value is passed to the function
[Increment -Wikipedia](https://ja.wikipedia.org/wiki/%E3%82%A4%E3%83%B3%E3%82%AF%E3%83%AA%E3%83%A1% E3% 83% B3% E3% 83% 88)
In C language, C ++, Java, JavaScript, etc., the increment operator (increasing quantum) "++" is prepared. There are two types, pre-increment and post-increment. The lexical word is the same "++", but the meaning is different depending on whether it is used as a prefix operator (example: ++ x) or as a suffix operator (example: x ++). If the value of the operand is an integer, it will be 1, and if it is a pointer, it will change by one target, but the value as an expression will be the value after incrementing if it is a prefix (this means +). = 1), in the case of postfix, it is the value before incrementing.
Kotlin source code.
var a = 1
var b = 1
println(a++)
println(++b)
Execution result. For a ++, 1 is output because it is passed to the function before the value is incremented by +1. In the case of ++ b, 2 is output because the value is incremented by +1 and then passed to the function.
$ kotlinc -script hello.kts
1
2
Kotlin's increment operator behaves like C and Java.
C source code.
#include <stdio.h>
int main(int argc, char *args[]) {
int a = 1;
int b = 1;
printf("%d\n", a++);
printf("%d\n", ++b);
return 0;
}
Execution result.
$ gcc hello.c -o hello
$ ./hello
1
2
Java source code.
public class Hello {
public static void main(String[] args) {
int a = 1;
int b = 1;
System.out.println(a++);
System.out.println(++b);
}
}
Execution result.
$ javac Hello.java
$ java Hello
1
2
Kotlin's increment operator overload is easy to write. You can overload the increment operator with operator fun inc.
data class Counter(val num: Int) {
operator fun inc(): Counter {
return Counter(num + 1)
}
}
var a = Counter(1)
var b = Counter(1)
println(a++)
println(++b)
Execution result. In the case of a ++, the value of num is passed to the function before it is incremented by 1, so the state of num = 1 is output. In the case of ++ b, the value of num is incremented by +1 and then passed to the function, so the state of num = 2 is output.
$ kotlinc -script counter.kts
Counter(num=1)
Counter(num=2)
Let's write the behavior of a similar increment operator in C ++. In C ++ you need to write separate functions for the suffix and the prefix.
#include <iostream>
class Counter {
private:
int num;
public:
Counter(int num) : num(num) {}
// Prefix increment operator
Counter& operator ++() {
this->num++;
return *this;
}
// Postfix increment operator
Counter operator ++(int) {
Counter c = *this;
this->num++;
return c;
}
friend std::ostream& operator<<(std::ostream& os, const Counter& c) {
os << c.num;
return os;
}
};
int main(int argc, char *argv[]) {
Counter a(1);
Counter b(1);
std::cout << a++ << std::endl;
std::cout << ++b << std::endl;
return 0;
}
Execution result.
$ g++ hello.cpp -o hellocpp
$ ./hellocpp
1
2
Recommended Posts