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

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

只是一遍又一遍地调用自己。我如何取消对它的对象的引用?

最佳答案

operator << 有 4 个重载他们相互依赖。

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

因此引起了无休止的回避。您应该至少将其中一个实现为 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 中实现输入类,否则你会看到编译时错误。

关于C++ 重载运算符,解引用引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23881126/

相关文章:

javascript - 数据属性危险吗?

c++ - LLVM 预处理器显然不支持 ## 运算符?

c++ - 我们可以在 C++ 类的成员函数中使用 cin>> 吗?

c++ - 重写模板化类函数

c++ - ADL 在特定情况下不起作用

c++ - 基类的继承和指针

c++ - 循环计数的有符号值与无符号值

C++ 函数包装器捕获特定信号

c++ - 即使条目相同,也找不到 std::map 键

C++:另一个类中的类作为类型?