开发者

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

开发者 https://www.devze.com 2023-01-07 05:13 出处:网络
How can I accomplish what is suggested by the following?: 开发者_如何学Pythontemplate<typename T>

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

精彩评论

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