LoginSignup
0
0

More than 5 years have passed since last update.

参照を返す関数について

Posted at

参照を返す関数の取り扱いについて、わからない点があったので、短いですが投稿します。

C++の以下のコードを実行したところ

int global_int=0;
int &f(){
    return global_int;
}

int main(){
    f()++;
    cout << global_int << "\n";
}

1

となりました。しかし次のコードを実行したところ

int global_int=0;
int &f(){
    return global_int;
}

int main(){
    int x;
    x = f();
    x++;
    cout << global_int << "\n";
}

0

となりました。この二つのコードが違う動作をする理由がわかりません。どなたか教えていただけないでしょうか?

0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0