c – 返回子类的const引用

c – 返回子类的const引用,第1张

概述我知道的 我知道返回一个临时对象的const引用是可以的! (像这个例子:) class A {public: virtual const A& clone () { return (A()); } virtual std::string name() const { return ("A"); }}; Returning temporary object and binding to 我知道的

我知道返回一个临时对象的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 ..但这个警告非常可怕……

编辑

谢谢你,我知道看到我的错误…但我想知道其他一些事情……

执行此 *** 作时,堆栈中到底发生了什么?

解决方法 将引用绑定到临时表会延长临时表的生命周期…除非它没有. §12.2[class.temporary] / p5,重点补充:

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()函数应返回指向堆分配副本的指针.

总结

以上是内存溢出为你收集整理的c – 返回子类的const引用全部内容,希望文章能够帮你解决c – 返回子类的const引用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1233999.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存