[英]Boost shared_ptr seems doesn't support operator ==
这是在Windows 7下的最新QT IDE上运行的(boost.1.48)
class Employee {
public:
int Id;
...
bool operator==(const Employee& other) {
qDebug() << this->Id << ":" << "compare with " << other.Id;
return this->Id==other.Id;
}
}
测试代码:
Employee jack1;
jack1 == jack1; // the operator== gets invoked.
shared_ptr<Employee> jack(new Employee);
jack == jack; // the operator== doesn't get invoked.
boost头文件中的相关代码是:
template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
{
return a.get() == b.get();
}
它似乎正在做指针比较,而不是做我期望的。
我做错了什么?
shared_ptr
是一个类似指针的类(它模拟具有额外功能的指针),因此operator==
for shared_ptr
比较指针。
如果你想比较指向的对象你应该使用*jack == *jack
,就像普通的指针一样。
尝试这个:
(*jack) == (*jack);
记得尊重你的指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.