使用模板化的“超级”

时间:2009-07-09 15:53:56

标签: c++

相关问题:Using "super" in C++

我检查了OpenSteer的源代码,并找到了以下用于定义车辆属性的代码。要求是Super也要从接口类AbstractVehicle继承。

template <class Super>
class SteerLibraryMixin : public Super { ... }

template <class Super>
class AnnotationMixin: public Super { ... }

template <class Super>
class LocalSpaceMixin: public Super { ... }

通过这些,我可以定义一个包含所有属性的SimpleVehicle:

// SimpleVehicle_1 adds concrete LocalSpace methods to AbstractVehicle
typedef LocalSpaceMixin<AbstractVehicle> SimpleVehicle_1;


// SimpleVehicle_2 adds concrete annotation methods to SimpleVehicle_1
typedef AnnotationMixin<SimpleVehicle_1> SimpleVehicle_2;


// SimpleVehicle_3 adds concrete steering methods to SimpleVehicle_2
typedef SteerLibraryMixin<SimpleVehicle_2> SimpleVehicle_3;


// SimpleVehicle adds concrete vehicle methods to SimpleVehicle_3
class SimpleVehicle : public SimpleVehicle_3 { ... }

或者仅使用其中一个属性:

class SimpleVehicle : public LocalSpaceMixin<AbstractVehicle> { ... }

这很聪明,在这里我可以选择我的车辆应具备的属性但是当我尝试编译它时它不起作用!

编译器错误:

SteerLibrary.hpp||In member function Vec2D SteerLibrary<Super>::SteerForWander(float)':|
SteerLibrary.hpp|41|error: there are no arguments toGetMaxSpeed' that depend on a template parameter, so a declaration of GetMaxSpeed' must be available|
SteerLibrary.hpp|41|error: (if you use-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)|
SteerLibrary.hpp|42|error: there are no arguments to GetVel' that depend on a template parameter, so a declaration ofGetVel' must be available|

这里我试图访问我的AbstractVehicle成员函数'GetMaxSpeed'和'GetVel'。 使用'-fpermissive'可以编译并工作,但它会为每个成员函数发出警告并且有充分的理由 - 可以很容易地从非AbstractVehicle继承,而不使用这些函数!

现在我的问题:这是一个更好的方法来处理这个问题吗?我尝试使用类似于上面链接的typedef方法,但它根本不起作用。

1 个答案:

答案 0 :(得分:4)

我认为这样做:

this->GetMaxSpeed()

将解决您的问题。你也可以这样做:

SimpleVehicle::GetMaxSpeed()

这两个都明确说明了函数的来源。