Why do un-named C++ objects destruct before the scope block ends?

前端 未结 5 1113
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 06:52

The following code prints one,two,three. Is that desired and true for all C++ compilers?


class Foo
{
      const char* m_name;
public:
      Foo(const char         


        
5条回答
  •  没有蜡笔的小新
    2020-11-29 07:17

    The scope of a temporary object like that is just one line. Think about it, you can no longer reference it after the line ends, so why would the object remain around?

    If this weren't the case, compilers wouldn't be able to optimize out temporary objects in function calls.

提交回复
热议问题