简体   繁体   English

返回子对象时,为什么延长了“子对象的完整对象”对象的寿命?

[英]Why is the lifetime of an object “that is the complete object of a subobject” extended when the subobject is returned?

I am studying the extension of the lifetime of a temporary when bound to a const & , and I would like to understand the following case: 我正在研究绑定到const &时的临时生存期的延长,并且我想了解以下情况:

#include <string>
#include <iostream>

char const * foo()
{
    std::string s("tmp");
    return s.c_str();
}

int main()
{
    char const * const & p = foo();

    // Why is the lifetime of the std::string extended in the following line,
    // rather than just the lifetime of the char* itself?
    std::cout << p; // This prints 'tmp' in VS2013
}

As noted, in Visual Studio 2013, the code builds without error and the console prints tmp . 如前所述,在Visual Studio 2013中,代码会正确生成,并且控制台显示tmp

This seems odd to me because the object whose lifetime is being extended is a subobject of an object local to the function being called that is destroyed when that function is exited. 这对我来说很奇怪,因为延长生命周期的对象是被调用函数局部对象的子对象 ,该对象在退出该函数时被销毁。 There is no std::string on the stack as the return value whose lifetime can be extended by the compiler when it compiles the main function - there's just a char * return value, whose lifetime can be extended but which would be a dangling pointer. 堆栈上没有std::string作为返回值,它的寿命可以由编译器在编译main函数时进行扩展-只有char *返回值,其寿命可以延长,但将是一个悬空的指针。

But clearly, the lifetime is being extended! 但显然,使用寿命正在延长!

The closest question I have found to this is here: Does "T const&t = C().a;" 我发现的最接近的问题是: “ T const&t = C()。a;”吗? lengthen the lifetime of "a"? 延长“ a”的寿命? ... However, in this question, the code B const& b = A().b; ...但是,在这个问题中,代码B const& b = A().b; references the complete object A on the stack inside the calling function, so that object is available to have its lifetime extended. 在调用函数内部引用堆栈上的完整对象A ,因此可以延长对象的生命周期。

As noted, it seems to me that the object whose lifetime should be extended in my code sample is a char * , not the string to which the char * points . 如前所述,在我看来,应该在我的代码示例中延长其生存期的对象是char * ,而不是char * points的字符串。 (That is to say, I would think the return value, being just the size of a char * , would itself have its lifetime extended, but that it would become a dangling pointer.) (也就是说,我认为返回值只是一个char *的大小,它的使用期限会延长,但它将变成一个悬空的指针。)

I do not understand how it is possible for the lifetime of the std::string to be extended in the sample code above. 我不明白如何在上述示例代码中延长std::string的生命周期。 Can someone explain why this satisfies the criteria of having the std::string 's lifetime extended by the char const * const & , rather than just having the char * 's lifetime extended? 有人可以解释为什么这满足通过char const * const &延长std::string的生存期,而不只是延长char *的生存期的条件吗?

How about following the constructor and destructor of your object 如何遵循对象的构造函数和析构函数

#include <string>
#include <iostream>

class string_like: public std::string {

public:
  string_like(const char* str): std::string (str) {
    std::cout << "string_like() : \n";
}
  ~string_like() {
    std::cout << "~string_like(): \n";
}
};

char const * foo()
{
  std::cout << "in foo(){} : \n" ;

  string_like  s("tmp");

  std::cout << "leaving foo(){}" << "\n";
  return s.c_str();
}


int main()
{
  std::cout << "begin main()\n";
  std::cout << "calling foo() :" << "\n";
  char const * const & p = foo();
  std::cout << "after calling foo() :\n";
  std::cout << "still in main\n" ;
  std::cout << p << "\n"; // print using g++
  std::cout << "leave main()\n";

}

I got the following output by g++ : 我通过g ++得到以下输出:

begin main()
calling foo() :
in foo(){} : 
string_like() :
leaving foo(){}
~string_like():  object is destroyed here
after calling foo() :
still in main
tmp    
leave main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM