繁体   English   中英

C ++重载运算符,取消引用参考

[英]C++ Overloading Operator, dereference reference

我已经在整个项目/类中继承了这个小的实用程序类(模板化)。

这个想法是,它允许轻松地将各种成员打包到类实例中和从类实例中打包(对于网络等,并不完全重要)

我所拥有的如下

    template<typename T>
    struct Packable
        /**
         * Packs a <class T> into a Packet (Packet << T)
         * Required for chaining packet packing
         *************************************************/
        virtual sf::Packet& operator <<(sf::Packet& packet) = 0; // Work-horse, must be defined by child-class

        friend sf::Packet& operator <<(sf::Packet& packet, const T *t)
        {
            // Call the actual one, but basically do nothing...
            return packet << *t;
        }

        friend sf::Packet& operator <<(sf::Packet& packet, const T &t)
        {
            // Call the actual one, but basically do nothing...
            return packet << &t;
        }

        friend sf::Packet& operator <<(sf::Packet& packet, T *t)
        {
            // Call the actual one, but basically do nothing...
            return packet << *t;
        }

        friend sf::Packet& operator <<(sf::Packet& packet, T &t)
        {
            // Call the actual one, but basically do nothing...
            return packet << &t;
        }
    };

简而言之,我正在尝试做的是使其在子类中只需指定/实现一种方法(用“虚拟”字表示)。

我想要的是提供采用各种形式的类的其他方法,并根据需要使用它们在编译类时将存在的虚拟方法进行解引用。

问题是我似乎已经创建了一些无限循环。

        friend sf::Packet& operator <<(sf::Packet& packet, T &t)
        {
            // Call the actual one, but basically do nothing...
            return packet << &t;
        }

只是一遍又一遍地称呼自己。 如何取消引用到对象的引用?

operator <<有4个重载,并且它们相互依赖。

  1. const T *,取决于2
  2. const T&,取决于1
  3. T *,取决于4
  4. T&,取决于3

因此,它引起了无尽的反响。 您应该至少将其中之一实现为独立功能 ,然后其余部分依赖于此。

您可以如下所示进行操作

template<typename T>
struct Packable
    /**
     * Packs a <class T> into a Packet (Packet << T)
     * Required for chaining packet packing
     *************************************************/
    virtual sf::Packet& operator <<(sf::Packet& packet) = 0; // Work-horse, must be defined by child-class

    friend sf::Packet& operator <<(sf::Packet& packet, const T *t)
    {
        // Call the actual one, but basically do nothing...
        return packet << *t;
    }

    friend sf::Packet& operator <<(sf::Packet& packet, const T &t)
    {
        // Call the actual one, but basically do nothing...
        //return packet << &t;

        // Serialize the contents into packet stream
        t.serialize(packet);
        return packet;
    }

    friend sf::Packet& operator <<(sf::Packet& packet, T *t)
    {
        // Call the actual one, but basically do nothing...
        return packet << const_cast<const T*>(t);
    }

    friend sf::Packet& operator <<(sf::Packet& packet, T &t)
    {
        // Call the actual one, but basically do nothing...
        return packet << &t;
    }
};

该函数serialize应在每个T类型类中实现,否则您将看到编译时错误。

暂无
暂无

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

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