c – 返回子类的const引用

我知道的

我知道返回一个临时对象的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&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...