简体   繁体   English

Boost shared_ptr似乎不支持operator ==

[英]Boost shared_ptr seems doesn't support operator ==

Here is the that runs on the latest QT IDE under Windows 7 (boost.1.48) 这是在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;
   }
}

testing code: 测试代码:

Employee jack1;
jack1 == jack1;   // the operator== gets invoked.

shared_ptr<Employee>  jack(new Employee);
jack == jack;  //  the operator== doesn't get invoked.

The related code in the boost header file is: boost头文件中的相关代码是:

template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
{
        return a.get() == b.get();
}

It seems that it is doing the pointer compare instead of doing the what I expect it. 它似乎正在做指针比较,而不是做我期望的。

What do I do wrong? 我做错了什么?

shared_ptr is a pointer-like class (it models a pointer with extra features) so operator== for shared_ptr compares pointers. shared_ptr是一个类似指针的类(它模拟具有额外功能的指针),因此operator== for shared_ptr比较指针。

If you want to compare the pointed-to objects you should use *jack == *jack , just like normal pointers. 如果你想比较指向的对象你应该使用*jack == *jack ,就像普通的指针一样。

try this: 尝试这个:

(*jack) == (*jack);

Remember to deference your pointers. 记得尊重你的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM