下記のサイト「Learn C++」;
https://www.programiz.com/cpp-programming/increment-decrement-operator-overloading
に、変数に対して前置きのオペレータと、後置きのオペレータについて例題を示して解説があります。
下記に引用するこの例題の中に、例えばオペレータが++
であれば、後置きの場合はオペレータ関数の定義時に、Class-Name operator ++ (int)
と記述する、とあります。
この理由に、
This int gives information to the compiler that it is the postfix version of operator.
Don't confuse this int doesn't indicate integer.
として、
「この括弧内のint
はinteger
の意味ではなく、コンパイラにこのオペレータが後置き型であることを示しているのだ」、
と言っています。
私は、int
はinteger
の意味、と学んできたので戸惑っています。
これについて、入門者向けの解説が出来る方に、ご案内をおねがいします。
html
1//引用する例題 2#include <iostream> 3using namespace std; 4class Check 5{ 6 private: 7 int i; 8 public: 9 Check(): i(0) { } 10 Check operator ++ () 11 { 12 Check temp; 13 temp.i = ++i; 14 return temp; 15 } 16 // Notice int inside barcket which indicates postfix increment. 17 Check operator ++ (int) 18 { 19 Check temp; 20 temp.i = i++; 21 return temp; 22 } 23 void Display() 24 { cout << "i = "<< i <<endl; } 25}; 26int main() 27 28{ 29 Check obj, obj1; 30 obj.Display(); 31 obj1.Display(); 32 // Operator function is called, only then value of obj is assigned to obj1 33 obj1 = ++obj; 34 obj.Display(); 35 obj1.Display(); 36 // Assigns value of obj to obj1, only then operator function is called. 37 obj1 = obj++; 38 obj.Display(); 39 obj1.Display(); 40 return 0; 41}
html
1//引用した例題の出力結果 2Output 3i = 0 4i = 0 5i = 1 6i = 1 7i = 2 8i = 1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/01/28 05:25