c++ - Boost shared_ptr 似乎不支持运算符 ==

标签 c++ qt boost operator-overloading shared-ptr

这是在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,就像普通指针一样。

关于c++ - Boost shared_ptr 似乎不支持运算符 ==,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8661791/

相关文章:

c++ - 双向链表实现

c++ - 高效的 C++ 条件自旋锁是否可能?

c++ - 静态库 (/MT) 链接与项目/MTd 选项冲突

qt - Windows上带有OpenCv 2.3.4的Qt Creator 5.0.1

boost - 如何通过 Gloa 使用来自多个文件的 boost 日志

c++ - 将数据插入有点复杂的数据结构 - C++

c++ - Qt 生成 Key Press(keyboard) to the system(OF) 一种跨平台的方式

c++ - 难以存储 QImage 然后恢复它

c++ - 我如何从没有复制构造函数的函数返回一个类?

c++ - 如何显式管理异构类型(和大小)的多个池分配器?