简体   繁体   English

C ++继承模板类

[英]C++ inherit template class

I've got a peculiar request, hopefully it's not too far fetched and can be done. 我有一个奇怪的要求,希望它不是太遥远,可以做到。

I've got a template class 我有一个模板类

    template<class T> class Packable
    {
    public:
        // Packs a <class T> into a Packet (Packet << T)
        virtual sf::Packet& operator <<(sf::Packet& p) const = 0;
        friend sf::Packet& operator <<(sf::Packet& p, const T &t);
        friend sf::Packet& operator <<(sf::Packet& p, const T *t);


        // Packs a Packet into a <class T> (T << Packet)
        virtual T operator <<(sf::Packet& p) const = 0;
        friend T& operator <<(T &t, sf::Packet& p);
        friend T* operator <<(T *t, sf::Packet& p);

        // Unpacks a Packet into a <class T> (Packet >> T)
        virtual sf::Packet& operator >>(sf::Packet& p) const = 0;
        friend sf::Packet& operator >>(sf::Packet& p, T &);
        friend sf::Packet& operator >>(sf::Packet& p, T* t);
    };

and I want to be able to use it something like 我希望能够像它一样使用它

class Player : public Packable
{
    //...
}

Such that Player now has all of those operators overloaded for it. 这样玩家现在已经为它重载了所有这些运算符。

As of now, I get Expected class name as a compile error. 截至目前,我将Expected class name作为编译错误。 Is there a way to accomplish this without needing to specify the class? 有没有办法在不需要指定类的情况下完成此操作? That is to say; 也就是说; how can Packable pick up Player as <class T> when I inherit it? 当我继承它时, Packable怎么能把Player当作<class T>

I guess you want something like the following: 我想你想要的东西如下:

class Player: public Packable<Player>
    //...

Yes, you need to do 是的,你需要这样做

template <typename T>
class Player: public Packable<T>
{
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM