问题说明了一切。
如果我要实现默认构造函数,使用 const 指针成员会更好吗?
最佳答案
需要使用成员初始化列表:
struct foo
{
const int& ref;
foo() : ref(some_value()) { }
}
确保 some_value()
不会给你一个临时值。它的生命只会延长到构造函数结束:
A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits.
关于c++ - 如何在默认构造函数中初始化引用成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15094416/