设计问题 - 一个函数调用所有三个

时间:2011-08-20 02:38:10

标签: c++

class Feature{};

class IFat
{
   //init feature for IFat

   vector<Feature> vf;
};
class IThin
{
   //init feature for IThin
   vector<Feature> vf;
};

class ISlim
{
    //init feature for ISlim
    vector<Feature> vf;
};

void func(IFeature_Vector)
{
     //accessing vf depending on IFeature_Vector passed in
}

我想知道是否有一个很好的方法来制作一个可以调用每个实例的func,而不必为每个案例调用三次。我很遗憾地说这是我知道这是一个jurasic问题,但我真的想不起一个好的解决方案,而我却为此疯狂。我希望你理解我的问题。如果你能提供帮助,我很感激。

1 个答案:

答案 0 :(得分:1)

我认为你应该看一下C ++中的template编程。这是一个很好的解释:http://www.cplusplus.com/doc/tutorial/templates/

你可以写点像

template<class T> void func(T myVector) { ... };