Boost库、序列化和一个与号运算符

The Boost library, serialization, and an ampersand operator?

本文关键字:一个 运算符 序列化 Boost      更新时间:2023-10-16

我来自Java和C#背景,作为深入C++的一种方式,我正在使用Qt和Boost构建一个图标对接。在查看序列化的文档时,我偶然发现了&操作人员

class gps_position
{
private:
    friend class boost::serialization::access;
    // When the class Archive corresponds to an output archive, the
    // & operator is defined similar to <<.  Likewise, when the class Archive
    // is a type of input archive the & operator is defined similar to >>.
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & degrees;
        ar & minutes;
        ar & seconds;
    }
    int degrees;
    int minutes;
    float seconds;

&操作员在阅读评论时非常清楚。我想知道的是,它是如何实现的?它怎么知道"&"应该是什么意思?我在谷歌上搜索了更多与号运算符的用法,但我能找到的只有&用来表示参考而不是反对。

谢谢。

重载位和的示例:

class Archive {
public:
   std::ostream& operator&( std::ostream& out ) {
       return out << to_string();
   }
   std::string to_string() { return "a string"; }
};

对于非内置类型,您可以在C++中定义运算符行为。

有关详细信息,请参阅C++常见问题解答。