C ++重载运算符,解引用引用

时间:2014-05-27 04:33:19

标签: c++ templates inheritance operator-overloading dereference

我有一个小实用程序类(模板化),我在整个项目/类中继承。

这个想法是它允许容易地将各种成员打包进出类实例(用于网络等,并不完全重要)

我得到的如下

    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;
        }

一遍又一遍地呼唤自己。如何取消引用它的对象?

1 个答案:

答案 0 :(得分:1)

operator <<有4次重载,它们相互依赖。

  1. const T *,取决于2
  2. const T&amp;,取决于1
  3. T *,取决于4
  4. T&amp;,取决于3
  5. 因此它引起了无休止的回忆。您应该至少将其中一个实现为independent functionality,然后其余部分依赖于此。

    您可以按照以下所示进行操作

    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类型类中实现,否则您将看到编译时错误。