我知道返回一个临时对象的const引用是可以的! (像这个例子:)
class A { public: virtual const A& clone () { return (A()); } virtual std::string name() const { return ("A"); } };
Returning temporary object and binding to const reference
但!
如果我想这样做,它仍然是正确的:
class B : public A { public: virtual const A& clone () { return (B()); } virtual std::string name() const { return ("B"); } };
我认为是的,但在执行时,返回的对象仍被视为A对象(如本例:)
main.cpp中
#include <iostream> #include <string> int main() { B bb; A* aa = &bb; std::cout << aa->clone().name() << std::endl; }
产量
valgrind ./a.out ==14106== Use of uninitialised value of size 8 ==14106== at 0x401BF9: main (main.cpp:8) ==14106== Uninitialised value was created by a stack allocation ==14106== at 0x401BF2: main (main.cpp:8) B
这是一个B .. yay ..但这个警告非常可怕……
编辑
谢谢你,我知道看到我的错误…但我想知道其他一些事情……
执行此操作时,堆栈中到底发生了什么?
解决方法
The temporary to which the reference is bound or the temporary that is
the complete object of a subobject to which the reference is bound
persists for the lifetime of the reference except:
- A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits.
- A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full-expression
containing the call.- The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not extended; the temporary is
destroyed at the end of the full-expression in the return statement.- A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the full-expression containing the
new-initializer.
你链接的问题中的情况(std :: string foo(); const std :: string& s = foo();)是可以的; foo()返回的临时生命周期延长到s的生命周期结束.在您的代码中,临时绑定到返回的值,并且根据上面的第三个项目符号点,它的生命周期不会延长,并且您的函数返回一个悬空引用.
通常来说,clone()函数应返回指向堆分配副本的指针.
原文地址:https://www.jb51.cc/c/115886.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。