0

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. 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.

Thanks.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Uri
  • 2,207
  • 2
  • 21
  • 21

2 Answers2

2

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"; }
};
perreal
  • 94,503
  • 21
  • 155
  • 181
0

For non-builtin types, You can define operator behaviour in C++.

For details, see the C++ FAQ.

Black
  • 5,022
  • 2
  • 22
  • 37