views:

563

answers:

5

Hello, I would like to compare two objects through their addresses. I tried operator overloading and it does not seem to work for pointers, but works for objects themselves. The following is the relevant code:

class C {
        public:
                int x;
};
.
.
.
bool operator <( C *ptr_c1, C *ptr_c2 )
{
        return ( (*ptr_c1).x < (*ptr_c2).x );
}
+3  A: 

A pointer is a native C++ type and you can only overload operators for user defined types (i.e. classes).

If this was possible it would violate the principal of 'least surprise' and cause pointer comparisons to behave differently based on whether not your definition was visible. This would probably be very confusing.

If you want to use an alternative comparison for use in container or with algorithms, note that you can provide a replacement for the default std::less in most associative containers and algorithms that require a partial order.

Charles Bailey
+1  A: 
if(*obj1 < *obj2) { /* this should use the overloaded operator. */ }
KiNgMaR
I have the pointers in a priority_queue that in turn uses '<'.
+3  A: 

See these:

Daniel Daranas
Thanks for the links. :)
+2  A: 

I tried operator overloading and it does not seem to work for pointers,

Correct.

but works for objects themselves.

Correct.

So what is the question?

Do you want to use pointers in a sorted container?

#include <iostream>
#include <set>

struct X
{
    int x;
};

struct XPTest
{
    bool operator()(X* const& lhs,X* const& rhs)
    {
        return lhs->x < rhs->x;
    }
};

int main()
{
    X       x1; x1.x    = 5;
    X       x2; x2.x    = 6;

    std::set<X*,XPTest> plop;

    plop.insert(&x1);
    plop.insert(&x2);
}
Martin York
A: 

Thank you very much for the suggestions. Now I realize I should have been a bit more specific for the need to say why I cannot use the beautiful solution suggested by KiNgMaR.

I am actually trying to keep track of objects in a STL priority_queue and update some internals while they are in the queue. (I would not change the priority itself.) But priority_queue works on operator overloading and when I tried that C++ objected. :(

I guess I will have to write my own complete code here implementing heaps and maps.

This doesn't make sense, either you're happy with the order provided my the objects that you are putting in the priority_queue`, in which case you must have an appropriate '<', or you are not and can provide an alternative comparison operator. Why do you need to implement new code for heaps and maps?
Charles Bailey
I have to process almost 1 billion words with the option of having to take out the ones with highest priority during some real time work based on some asynchronous requirement. Weird, I know. :|
@snjv: I think that with STL priority queue, you can add a compare trait of your own. You can probably use a plain function that takes a pair of pointers.
quamrana
@snjv: You can alway post another question showing us how far you got with the priority_queue and operator overloading.
quamrana
@quamrana: Thanks a lot, it helped like a charm. :) I also just realized that map is powerful enough that it can be tweaked as a priority queue that can update the priority itself! O(log_n) run-time for top() in return to O(log_n) change_priority() (to increase or decrease) is definitely affordable. :D