views:

63

answers:

2

I have a situation where I would like to compare an object encapsulated by a shared_ptr with the same type of object created on a stack. Currently, I'm getting the raw pointer and dereferencing it to do the comparison eg:

Object A;
std::shared_ptr<Object> B;

// assume class Object has its comparison operators overloaded
if ( *B.get() < A )
    // do stuff here

Is there a better way to do this? That is assuming that when both objects meet to be compared with each other, one is a shared_ptr and the other is not.

+2  A: 

That looks right to me. It's a pointer. To compare what it points to to something else, you need to dereference it.

bmargulies
+10  A: 

shared_ptr overloads operator*() so that it acts just like a pointer, so just write:

if ( *B < A ) {

docs: http://www.boost.org/doc/libs/1_42_0/libs/smart_ptr/shared_ptr.htm#indirection

olooney
sweet! much better.
Seth