【发布时间】:2016-08-31 09:58:08
【问题描述】:
请帮我解决以下问题:
我有一个类声明为:
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> 类中调用DummyEquation() 模板化方法。但它的实现方式如列出的那样不起作用 - 编译器在尝试强制转换为派生类时报告此指针的 constness 问题...
如何让它工作?我已经尝试了几乎所有可能的 const 限定符放置,但没有解决我的问题。还要提一件事 - 方法 create<>() 不能被限定为 const ( create<>() const ),因为它修改了它的所有者类的内容......
非常感谢任何愿意帮助我的人......干杯马丁
【问题讨论】:
-
DummyEquation 如果要通过 const 指针调用,则需要为 const 方法。