Boost库、序列化和一个与号运算符
The Boost library, serialization, and an ampersand operator?
我来自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++常见问题解答。
相关文章:
- 一个关于在C++中重载布尔运算符的问题
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- 为 std::variant 提供一个运算符 ==
- 一元*运算符的操作数是否期望一个 prvalue
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 运算符重载:"operator+"必须采用零个或一个参数
- 重载运算符*以获取对另一个类的实例的引用
- 使用 scope 运算符 (::) 引用另一个文件中的类
- 我重载了 << 和 = 运算符。为什么当我将一个对象分配给另一个对象并尝试打印它时,我会被打印出来?
- 在一个指令中声明更多指针的运算符优先级
- 为什么有条件编译运算符模板会更改另一个运算符的可用性?
- 我重载了一个运算符*()函数来计算两个矩阵的总和,但编译器提示此错误
- 基于范围的循环使用另一个运算符
- 在C++中,我可以使用箭头运算符访问另一个运算符吗
- 卡住了---用C++创建一个运算符模板
- 为什么一个运算符的操作数需要具有相同的类型
- 是否可以为 C++ 的字符串类创建一个运算符+函数?并连接"literals"?
- 如何为 boost::tuple 编写一个 '<<' 运算符?