继承的引用成员的构造函数中的C++默认初始化
C++ default initialization in constructor of an inherited reference member
我有一个更新外部引用的基类,我想构建一个继承类,将此引用作为成员嵌入。引用的一种默认初始化。
我想出了以下解决方案:
#include<iostream>
class Statefull
{
public:
Statefull( int& ref ) : _base_ref(ref) {}
int& _base_ref;
// update the extern variable
void work() { std::cout << ++_base_ref << std::endl; }
};
class Stateless : public Statefull
{
public:
// use a temporary allocation
Stateless( int* p = new int() ) :
// we cannot initialize local members before base class:
// _dummy(), Statefull(_dummy)
// thus, initialize the base class on a ref to the temporary variable
Statefull(*p),
_tmp(p),
_dummy()
{
// redirect the ref toward the local member
this->_base_ref = _dummy;
}
int* _tmp;
int _dummy;
// do not forget to delete the temporary
~Stateless() { delete _tmp; }
};
int main()
{
int i = 0;
Statefull full(i);
full.work();
Stateless less;
less.work();
}
但是,在构造函数的默认参数中需要一个临时分配似乎相当丑陋。有没有一种更优雅的方法可以实现这种默认初始化,同时在基类构造函数中保留引用?
好吧,Stateless
类违反了三规则。但我认为这是因为这只是展示真正问题的示例代码。
现在,为了真正解决这个问题:将引用绑定到未初始化的变量是完全有效的,只要在初始化之前没有使用它的值。
Stateless() : Statefull(_dummy), _dummy() {}
目前的解决方案有效,但似乎对为什么有效存在一些误解。
// redirect the ref toward the local member
this->_base_ref = _dummy;
不能"重定向"引用。引用只能绑定一次:在初始化时。分配给引用将分配给它所引用的对象。在这种情况下,this->_base_ref = _dummy
与*_tmp = _dummy
完全相同:它将_dummy
的值分配给*_tmp
。然而,_base_ref
仍然指*_tmp
(您可以用assert(&_base_ref == tmp)
来测试这一点)。
我认为这可能有效:
StateLess(): Statefull(*new int) {}
~StateLess() { delete &_base_ref; }
你不能没有临时性,但它们不一定在类定义中。
使用更多的类可以解决所有问题
class StateForStateful
{
protected:
int state;
};
class Stateless: private StateForStateful, public Stateful // order is important
{
public:
Stateless():Stateful(this->state) {}
};
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 如何使用默认参数等选择模板专业化
- 具有默认模板参数的多态类的模板推导失败
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 初始化具有非默认构造函数的std::数组项的更好方法
- 何时提供默认参数作为模板参数
- 是默认情况下分配给char数组常量的值
- 具有默认值的引用获取函数
- 具有默认模板类型的默认构造函数的类型推导
- 当给定默认值时,为什么此模板参数推导失败
- 修改 VS Code 中的默认C++代码段
- 声明默认的模板化函数
- 将const引用参数初始化为默认参数会导致悬空引用吗
- 如何使用非默认构造函数实例化模板化类
- 如何修复带有 clang 的参数'args'缺少默认参数的问题?
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 为什么大多数 pair 实现默认不使用压缩(空基优化)?
- 格式化浮点值:返回默认值
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr