c – 返回子类的const引用

前端之家收集整理的这篇文章主要介绍了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&C++相关文章