简体   繁体   English

Boost库,序列化和&符号运算符?

[英]The Boost library, serialization, and an ampersand operator?

I come from a Java and C# background and as a way to dive into C++, I'm building an icons dock using Qt and Boost. 我来自Java和C#背景,作为一种潜入C ++的方式,我正在使用Qt和Boost构建一个图标停靠。 Looking at the documentation for the serialization, I stumbled uppon some interesting use of an & operator. 看一下序列化的文档,我偶然发现了一些有趣的&运算符。

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;

The purpose of the & operator is very clear reading the comments. &运算符的目的非常清楚阅读注释。 What I'd like to know, is how is it achieved? 我想知道的是,它是如何实现的? How does it know what "&" is supposed to mean? 它是如何知道“&”应该是什么意思? I searched for more uses of the ampersand operator on Google, but all I can find is & used in order to denote reference rather than an opperation. 我在Google上搜索了&符号运算符的更多用法,但我能找到的只是用于表示引用而不是操作。

Thanks. 谢谢。

An example for overloading bitwise-and: 按位重载的示例 - 和:

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

For non-builtin types, You can define operator behaviour in C++. 对于非内置类型,您可以在C ++中定义操作符行为。

For details, see the C++ FAQ . 有关详细信息,请参阅C ++ FAQ

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

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