c++ - What does the ampersand do here?
问题描述
I'm trying to understand this section of code, and I've come across something I just cannot interpret:
template<unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM>
void CellBasedSimulationArchiver<ELEMENT_DIM, SIM, SPACE_DIM>::Save(SIM* pSim)
{
// Do a bunch of stuff
boost::archive::text_oarchive* p_arch = arch_opener.GetCommonArchive();
// Archive the simulation (const-ness would be a pain here)
(*p_arch) & pSim; //<--------------- What is this?
}
The point of this function is to archive the state of a simulation using boost, so it can be reloaded at a later time and continued. I'm trying to understand how the archiving actually happens, but the line with the arrow baffles me. At some point the actual saving has to happen, and I guess the arrow is where it happens.
Something must be going on with boost, but what exactly does this line mean and what does the &
do here? Is it some kind of reference? I looked at some documentation and it contains the <<
operator, but nowhere is there a &
解决方案
这里的 & 符号是二元&
运算符。对于整数类型,这将是按位与运算符(就像<<
左移运算符一样),但boost::archive::text_oarchive
不是整数类型。必须为此类定义了运算符重载函数。因此,此运算符将调用该函数。
根据文档:
sa << x sa & x
这些表达式必须执行完全相同的功能。他们将 x 的值与其他信息一起附加到 sa。此其他信息由存档的实现定义。通常,此信息是相应加载存档类型正确恢复 x 值所需的信息。
因此,(*p_arch) & pSim;
附加pSim
到p_arch
.
推荐阅读
- react-native - react-native-push-notification 无法在 android 中构建
- javascript - Sinon Stub JavaScript 方法链
- angularjs - Angularjs uib-collapse 表列隐藏和显示的行为
- xml - Linearlayout messed up with the horizontal alignment
- python - 如何在决策树中获取特征重要性?
- php - 如何根据 WordPress 中的当前每周日期设置默认类别?
- javascript - access count variable defined in setinterval javascript
- python-3.x - Print output to separate text files
- angular - 为什么我的反应式表单验证在 Angular 中失败
- javascript - 使用 JavaScript 进行媒体查询