覆盖函数参数 /
Override function parameters /
假设我们有一个集合类,如下所示:
class CCollection {
public:
void add(B& b);
void remove(B& b);
void doSomethingWithAllObjects();
protected:
std::vector<B*> bs;
}
其中 B 是一个抽象类和
doSomethingWithAllObjects();
取决于B的具体类型,称之为C。
有没有办法推导出CCollection并让方法
add(B b);
remove(B b);
只接受派生类型?
我想到了像这样覆盖方法的事情:
class D : A{
public:
void add(C c);
void remove(C c);
void doSomethingWithAllObjects();
private:
std::vector<B*> bs;
}
或通用的爪哇结构,如
template<class T : B>
class C {
...//do lots of stuff
}
推导几乎 100% 相同。但你不能混合B的不同派生。
我已经读过将模板类限制为某些类型几乎是不可能的,但必须有一种方法可以避免为 B 的每个派生编写整个类。关键是,我需要 B 中定义的函数,所以我不能使用简单的模板
template<class T>
class B{
....
}
当然,我可以假设其他程序员只是将正确的类型交给正确的CCollection,但这不可能是精神。我想要的是强迫其他程序员只添加一种类型的B。
我不确定我是否正确理解,但我认为您正在寻找一个简单的模板非成员类型函数。模板函数可用于确保类型匹配。
template<typename T>
void global_adder(const T& cl, const T& toadd) {
cl.add(toadd);
}
由于不进行基于继承的类型推断,这将确保 A 未添加到 B 或 B 添加到 C 等。待添加两个参数必须具有相同的类型。而已。
仅将类方法保留在基类中。来得及
protected
并将此功能添加为friend
。这样,您现在就可以从中拨打
a.add
或b.add
其他地方(将无法为某个添加不同的类型类(。添加或删除元素的唯一方法是通过模板确保类型匹配的函数。
你可以创建一个抽象的基集合类,如
class BaseCollection {
public:
void doSomethingWithAllObjects();
protected:
void addInternal(B* b); // consumes the element
std::vector<B*> bs; // or better use vector<shared_ptr> for reference count
};
template <typename C>
class Collection : public BaseCollection {
public:
void add(const C& c) {
C* my_copy = new C(c); // suppose we have a copy constructor
addInternal(my_copy);
}
};
如果您尝试实例化Collection<C>
其中C
不是 B
的子类,则会收到编译错误。
相关文章:
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类