C++ syntax/legality of inheriting from a template while also specifying type
How can I accomplish what is suggested by the following?:
开发者_如何学Pythontemplate<typename T>
class Base{...};
...
class Derived : public Base<int>{...};
Yes, it's legal.
Base
is a class template, and when provided all its template parameters it can be instantiated, which makes it an instantiated class. So Base<int>
is a class name, and you can inherit from it just fine.
That looks good.
There's a specific idiom associated with this called static inheritance.
template<typename T> class Base {
void MyStaticVirtualFunction() { T::MSVF(); }
};
class Derived : public Base<Derived> {
void MSVF();
};
In this case Base acts as a base class at compile-time and the function call is resolved statically, but the behaviour of Base can still vary.
精彩评论