c++ - What is definition of reference type? - Stack Overflow

PHOTO EMBED

Thu Aug 18 2022 14:50:00 GMT+0000 (Coordinated Universal Time)

Saved by @Marcos_ #cpp

{ // Block scope
     Foo fooVal = makeFoo(); // Say makeFoo() returns a (temporary, unnamed) Foo
     // Here the temporary Foo is dead (fooVal is a copy).

     // Foo &fooRef = makeFoo(); // Error, reference is non-const
     Foo const &fooCRef = makeFoo(); // All good

     // ...

     // The second temporary is still alive
     fooCRef.doSomethingFunny(); // Works like a charm !

} // The second temporary dies with fooRef

//------------------------------------------

Foo *fooPtr = new Foo; // Here is a Foo
Foo &fooRef = *fooPtr; // Here is an alias for that Foo

delete fooPtr; // Here is the end of that Foo's life

fooRef.doSomethingFunny(); // Here comes trouble...
content_copyCOPY

https://stackoverflow.com/questions/24827592/what-is-definition-of-reference-type