私はC ++でポインター変数と参照変数を学習していますが、サンプルコードがあります。* cの値が33から22に変更された理由がわかりません。誰かがプロセスを理解するのを手伝ってくれませんか?
int a = 22;
int b = 33;
int* c = &a; //c is an int pointer pointing to the address of the variable 'a'
int& d = b; //d is a reference variable referring to the value of b, which is 33.
c = &b; //c, which is an int pointer and stored the address of 'a' now is assigned address of 'b'
std::cout << "*c=" << *c << ", d=" << d << std::endl; //*c= 33 d= 33
d = a; //d is a reference variable, so it cannot be reassigned ?
std::cout << "*c=" << *c << ", d=" << d << std::endl; //*c= 33 d= 33
d = a; //d is a reference variable, so it cannot be reassigned ?
それは誤解です。このステートメントは、a
(22)の値を()へのd
参照である変数に割り当てますb
。それは何d
への参照であるかを変えます。したがって、その行が実行された後、の値b
は22になります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加