[英]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.