Reference to an unnamed temporary object (life time)

前端 未结 3 1390
醉话见心
醉话见心 2021-01-14 00:04

After reading this answer from ildjarn, I wrote the following example, and it looks like an unnamed temporary object has the same life time as its reference!

  • H
3条回答
  •  不思量自难忘°
    2021-01-14 00:23

    Lightness Races in Orbit is right. And I think this example would be more concise.

    #include   //cout
    #include 
    
    int main ()
    {
        using namespace std;
        int a = 123;
        int b = 123;
    //  int       & a_b = a + b; // error!
        int const & a_b = a + b;
        cout<<"hello world!"<

提交回复
热议问题