C ++继承模板类 [英] C++ inherit template class

查看:244
本文介绍了C ++继承模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个模板类

$ b。我有一个特殊的请求,希望不会太过分。
$ b

 模板< class T> class Packable 
{
public:
//打包< class T> (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);


//将分组打包到< class T> (T virtual T operator<<(sf :: Packet& p)const = 0;
friend T& operator<<(T& t,sf :: Packet& p);
friend T * operator<<(T * t,sf :: Packet& p);

//将包解包到< 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);
};

,我想要使用

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

现在,Player现在有所有的操作符重载了。



get 期望的类名作为编译错误。有没有办法完成这个而不需要指定类?也就是说;如何可包装拾取玩家< class T>

我想你想要的东西像下面这样:

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


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. 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?

解决方案

I guess you want something like the following:

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

这篇关于C ++继承模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆