Der*_*ang 2 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;
}
}
Run Code Online (Sandbox Code Playgroud)
测试代码:
Employee jack1;
jack1 == jack1; // the operator== gets invoked.
shared_ptr<Employee> jack(new Employee);
jack == jack; // the operator== doesn't get invoked.
Run Code Online (Sandbox Code Playgroud)
boost头文件中的相关代码是:
template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
{
return a.get() == b.get();
}
Run Code Online (Sandbox Code Playgroud)
它似乎正在做指针比较,而不是做我期望的.
我做错了什么?
CB *_*ley 16
shared_ptr是一个类似指针的类(它模拟具有额外功能的指针),因此operator==用于shared_ptr比较指针.
如果你想比较你应该使用的指向对象*jack == *jack,就像普通指针一样.
| 归档时间: |
|
| 查看次数: |
2363 次 |
| 最近记录: |