C++: Reference to “out of scope” object

前端 未结 3 742
慢半拍i
慢半拍i 2020-12-20 18:33

There is one thing I never understood about references and I hope that one might help me. For all I know, a reference cannot be null. But what happens if you have a function

3条回答
  •  生来不讨喜
    2020-12-20 18:46

    The behaviour in this context is undefined - this isn't particularly odd in c++. This is essentially identical to the situation where you have a pointer set to a local variable which has gone out of scope. C++ requires YOU control handle references and the lifetime of referenced objects.

提交回复
热议问题