Guaranteed lifetime of temporary in C++?

后端 未结 5 2061
天涯浪人
天涯浪人 2020-11-22 12:22

Does C++ provide a guarantee for the lifetime of a temporary variable that is created within a function call but not used as a parameter? Here\'s an example class:

5条回答
  •  时光取名叫无心
    2020-11-22 12:51

    StringBuffer is in the scope of GetString. It should get destroyed at the end of GetString's scope (ie when it returns). Also, I don't believe that C++ will guarantees that a variable will exist as long as there is reference.

    The following ought to compile:

    Object* obj = new Object;
    Object& ref = &(*obj);
    delete obj;
    

提交回复
热议问题