【发布时间】:2017-04-05 17:14:58
【问题描述】:
是否有可能实现这样的行为?它不必使用继承,我只想通过泛型参数传递(使用 c++ 模板)实现模板方法设计模式。
class A {
public:
template<typename ...tArgs>
void call(tArgs ...vArgs) {
for (int i = 0; i < 5; ++i)
impl(vArgs...);
}
};
class B : public A {
public:
void impl() {}
void impl(int) {}
};
int main() {
B b;
b.call(); // ok
b.call(1); // ok
b.call(""); // compile error, no method to call inside 'call'
return 0;
}
【问题讨论】:
-
不太清楚你想要实现什么。在您的示例中,
const char*在编译时没有重载,因此您会收到编译错误。 -
That's not the only error in that code. 这些对
call的调用都不起作用,因为impl无法解析。 -
这个例子不起作用,没有最后一次调用的事件。我只描述了期望的行为。
标签: c++ templates template-method-pattern