最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Comparison of pointers QSharedPointer<T> and T* - Stack Overflow

programmeradmin3浏览0评论

I am trying to compare a QSharedPointer with a raw pointer to the underlying object:

MyClass* object = new MyClass;
QSharedPointer<MyClass> sharedObject (object);

if(object == sharedObject)
   return true; // If Equal

Will this work correctly?

I am trying to compare a QSharedPointer with a raw pointer to the underlying object:

MyClass* object = new MyClass;
QSharedPointer<MyClass> sharedObject (object);

if(object == sharedObject)
   return true; // If Equal

Will this work correctly?

Share Improve this question edited Mar 14 at 12:05 wohlstad 30k17 gold badges61 silver badges93 bronze badges asked Mar 14 at 11:34 Andrey StrokovAndrey Strokov 397 bronze badges 3
  • 2 Have you tried it? How did it work in your attempt? What happened? What did you expect to happen? – Some programmer dude Commented Mar 14 at 11:42
  • What does "correctly" mean? What happens when you run it? Do you want to compare the object is the same? When using different objects, do you want to compare if the data contained in both objects is equal? – Friedrich Commented Mar 14 at 11:42
  • And what is the actual and underlying problem you need to solve? Why do you need this comparison? What should it actually compare? The pointers or the objects? And how will such a comparison solve your actual problem? – Some programmer dude Commented Mar 14 at 11:48
Add a comment  | 

1 Answer 1

Reset to default 2

Yes it is valid, because QSharedPointer has a related operator==:

template <typename T, typename X> 
bool operator==(const T *ptr1, const QSharedPointer<X> &ptr2)

That compares a raw pointer to a QSharedPointer and:

Returns true if the pointer ptr1 is the same pointer as that referenced by ptr2.

So in your case (object == sharedObject) will be true.

Note:
The operands can also be compared in the opposite order, because there is also:
template <typename T, typename X> bool operator==(const QSharedPointer<T> &ptr1, const X *ptr2).

发布评论

评论列表(0)

  1. 暂无评论