gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:17:43 25 4
gpt4 key购买 nike

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
}

我想知道是否有一种很好的简洁方法来制作一个可以调用每个实例而不必为每个案例调用三次的函数。我很抱歉这么说,我知道这是一个侏罗纪问题,但我就是想不出一个好的解决方案,而我为此非常疯狂。我希望你能理解我的问题。如果您能提供帮助,我将不胜感激。

最佳答案

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

你可以这样写

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

关于c++ - 设计问题——一个函数调用所有三个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7129262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com