这是否会导致引用超出范围后指向任何内容?

owa*_*agh 0 c++ constructor scope reference

以下代码是否安全?

class B {
  public:
    int& b;
    B (int& _b) :
      b(_b) {}
};

B* foo() {
  int a;
  return new B(a);
}
Run Code Online (Sandbox Code Playgroud)

foo返回的对象中的引用是否会指向任何内容(因为int a超出范围)或编译是否计算出来?

Ker*_* SB 5

编译器可能会警告您,但新创建的对象肯定包含悬空的无效引用,因为对象a在范围的末尾停止存在foo.