为什么直接传递"this"指针来存档是一个错误,而另一个相同类型的指针是可以的?
Why is passing 'this' pointer directly to archive an error, but another pointer of same type is ok?
传递分配给另一个指针的this
指针工作正常,但直接传递它本身不会如下所示:
table_row* table_row::deserialize_row(std::string src_serialized_row) {
std::stringstream ss(src_serialized_row);
boost::archive::text_iarchive ia(ss);
table_row * dest = this;
ia >> dest; // this is fine, compiles.
return dest;
}
table_row* table_row::deserialize_row(std::string src_serialized_row) {
std::stringstream ss(src_serialized_row);
boost::archive::text_iarchive ia(ss);
ia >> this; //error, >> operator does not match [error]
return this;
}
[错误] 我真的不明白这一点。我在两个代码示例中传递相同的指针,对吗?为什么会是一个错误?
唯一的区别是this
是一个 prvalue,将其分配给dest
将使其成为左值。
我假设运算符看起来像这样:
template<class T>
boost::archive::text_iarchive& operator>>(
boost::archive::text_iarchive& ia,
T& archive_to
);
像this
这样的右值无法绑定到非 const 左值引用,因为它试图将指针设置为反序列化值(可能不是您想要的(。
相关文章:
- 为什么我在空指针错误(链表)中获取成员访问权限
- std::函数指针错误:无法将 &A::a 转换为类型 std::function<>&&
- 为什么我得到 munmap_chunk():无效的指针:错误
- 成员模板函数指针错误
- 删除指针时,未分配被释放的 C++ 指针错误
- Visual Studio 中的"指向引用的指针"错误不会按发生位置的文件名显示
- C++数组指针错误无法将“int*”转换为“int**”
- C++ 通过函数指针错误调用成员函数
- 从 BST 中删除元素(指针错误)
- 随机生成的多维布尔指针错误
- 在测试中使用unique_ptr时指针错误无效
- 使用 malloc 而不是 new 会导致 free():指针错误无效
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- 抽象类和唯一指针 c++ 错误
- 返回函数指针错误而不带类型定义
- 练习中出现空指针错误
- 指针错误导致的分段错误(简单..)
- free():C++中的指针错误无效
- 使用布尔值数组"表达式必须具有指向对象类型的指针"错误C++
- 指针错误的C 数组