开发者

C++ syntax/legality of inheriting from a template while also specifying type

How can I accomplish what is suggested by the following?:

开发者_如何学Python
template<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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜