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
1 Answer
Reset to default 2Yes 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)
.