且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C++,多态与函数参数的模板化

更新时间:2023-11-02 17:17:52

嗯,首先,模板不是运行时多态——它们是编译时多态.

Uhm, first of all, templates are not runtime polymorphism -- they're compile-time polymorphism.

如果你想使用运行时多态性,你必须确保 ModCont> 派生自 ModCont<A<T>> -- C++ 处理多态的方式并没有使它成为默认值.或者,您可以让所有 ModCont<T> 派生自一些通用的 ModContBase,尽管目前还不清楚它是如何工作的.

If you want to use runtime polymorphism with this, you have to make sure that ModCont<B<T> > derives from ModCont<A<T> > -- the way C++ handles polymorphism does not make this the default. Alternatively, you could have all ModCont<T> derive from some general ModContBase, although it's unclear how that would work.