CRTP:从基础调用派生 class 模板化方法
CRTP: Call derived class templated method from base
请帮我解决以下问题:
我有一个 class 声明为:
template<typename GEAR_TYPE>
class Rim
: /* Derive from GenericComponent Design perspective using CRTP */
public Design::GenericComponent<Rim<GEAR_TYPE>>
{
public:
template<typename IDENTIFICATION_TAG>
typename Base::Parameter<typename IDENTIFICATION_TAG::UnitType, typename IDENTIFICATION_TAG::DataType>::QuantityType & DummyEquation( void )
{
return( Base::Parameter<typename IDENTIFICATION_TAG::UnitType, typename IDENTIFICATION_TAG::DataType>::QuantityType::from_value( 222 ) );
}
};
使用 CRTP 从 Design::GenericComponent<>
继承。在 Design::GenericComponent<> 中有一个方法:
template<typename DERIVED_COMPONENT_TYPE>
class GenericComponent
{
public:
template<typename PARAM_IDENTIFICATION>
std::shared_ptr<Base::Parameter<typename PARAM_IDENTIFICATION::UnitType, typename PARAM_IDENTIFICATION::DataType>> get( void ) const
{
mParameters.template create<PARAM_IDENTIFICATION>( static_cast<const DERIVED_COMPONENT_TYPE *>( (this) )->template DummyEquation<PARAM_IDENTIFICATION>() );
}
};
GenericComponent
中的 get()
方法应该调用派生的 Rim<GEAR_TYPE>
class 中的 DummyEquation()
模板化方法。但是它所列的实现方式不起作用 - 编译器在尝试转换为派生 class...
时报告此指针的常量性问题
如何让它工作?我已经尝试了几乎所有可能的 const 限定符放置,但没有解决我的问题。再提一件事 - 方法 create<>()
不能限定为 const
( create<>() const
),因为它修改了它的所有者 class...[=21= 的内容]
非常感谢任何愿意帮助我的人...干杯马丁
this
是指向 const
对象(在 get
范围内)的指针。将其沿继承层次结构向下转换不会更改结果对象的 const
-ness。
因此 DummyEquation
& create
需要是 const
成员函数,或者 get
需要是非 const
.
请帮我解决以下问题:
我有一个 class 声明为:
template<typename GEAR_TYPE>
class Rim
: /* Derive from GenericComponent Design perspective using CRTP */
public Design::GenericComponent<Rim<GEAR_TYPE>>
{
public:
template<typename IDENTIFICATION_TAG>
typename Base::Parameter<typename IDENTIFICATION_TAG::UnitType, typename IDENTIFICATION_TAG::DataType>::QuantityType & DummyEquation( void )
{
return( Base::Parameter<typename IDENTIFICATION_TAG::UnitType, typename IDENTIFICATION_TAG::DataType>::QuantityType::from_value( 222 ) );
}
};
使用 CRTP 从 Design::GenericComponent<>
继承。在 Design::GenericComponent<> 中有一个方法:
template<typename DERIVED_COMPONENT_TYPE>
class GenericComponent
{
public:
template<typename PARAM_IDENTIFICATION>
std::shared_ptr<Base::Parameter<typename PARAM_IDENTIFICATION::UnitType, typename PARAM_IDENTIFICATION::DataType>> get( void ) const
{
mParameters.template create<PARAM_IDENTIFICATION>( static_cast<const DERIVED_COMPONENT_TYPE *>( (this) )->template DummyEquation<PARAM_IDENTIFICATION>() );
}
};
GenericComponent
中的 get()
方法应该调用派生的 Rim<GEAR_TYPE>
class 中的 DummyEquation()
模板化方法。但是它所列的实现方式不起作用 - 编译器在尝试转换为派生 class...
如何让它工作?我已经尝试了几乎所有可能的 const 限定符放置,但没有解决我的问题。再提一件事 - 方法 create<>()
不能限定为 const
( create<>() const
),因为它修改了它的所有者 class...[=21= 的内容]
非常感谢任何愿意帮助我的人...干杯马丁
this
是指向 const
对象(在 get
范围内)的指针。将其沿继承层次结构向下转换不会更改结果对象的 const
-ness。
因此 DummyEquation
& create
需要是 const
成员函数,或者 get
需要是非 const
.