Sign in to follow this  
saeedm

Overloading the less than operator

Recommended Posts

I'm trying to overload the < operator in my class Vertex so that my make_heap function can sort them by their .getDist() value. My problem is I run into errors since my heap actually contains Vertex pointers. How should I overload the < operator, or should I try another approach? The value I need to compare is accessed like this: Vertex * v; v->getDist(); Thanks for any help

Share this post


Link to post
Share on other sites
Quote:
Original post by saeedm
I'm trying to overload the < operator in my class Vertex so that my make_heap function can sort them by their .getDist() value. My problem is I run into errors since my heap actually contains Vertex pointers. How should I overload the < operator, or should I try another approach? The value I need to compare is accessed like this:

Vertex * v;
v->getDist();

Thanks for any help
You could try creating a global (free) operator<() function that takes two Vertex pointers as arguments (I'm pretty sure this would work).

Also, did you explain in your other thread why you're storing pointers to vertices rather than just storing the vertices themselves? (I'm assuming you have a good reason for doing this...)

Share this post


Link to post
Share on other sites
Since your comparing to classes that are the same, and using a member variable to to determine which Vertex (class) is less then the other Vertex (class), you can just overload the < operator in the class. That way when your do a comparison VertexA < VertexB, it will use the classes < operator. This way you don't have to use your access function for comparisons.


template <class T>
class Vertex {
private:
float distance;

T x, y, z;

// other members here

public:
Vertex(T x, T y, T z);
~Vertex();

// other class methods here

void SetDistance(float d) { distance = d; } // just for this example

bool operator < (const Vertex<T>& right) {
return this->distance < right.distance;
}
};


Vertex<int>* a = new Vertex<int>(1, 2, 3);
Vertex<int>* b = new Vertex<int>(1, 2, 3);

a->SetDistance(10.0f);
b->SetDistance(11.0f);

if(a < b)
std::cout << "Hey" << std::endl;


Hope this helps

Share this post


Link to post
Share on other sites
Quote:
Original post by Atom
Since your comparing to classes that are the same, and using a member variable to to determine which Vertex (class) is less then the other Vertex (class), you can just overload the < operator in the class. That way when your do a comparison VertexA < VertexB, it will use the classes < operator. This way you don't have to use your access function for comparisons.
The OP is not storing Vertex objects, but rather pointers to Vertex objects.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
You could try creating a global (free) operator<() function that takes two Vertex pointers as arguments (I'm pretty sure this would work).
Overloaded operators must have at least one parameter of class type, reference to class type, enumeration type or reference to enumeration type.

OP: If you really do need to be working with pointers then you should use the forms of make_heap and friends which take a predicate and use this predicate to define your sort order:
struct less_vector_pointer
:
public std::binary_function< Vertex const *, Vertex const *, bool >
{

bool operator()(Vertex const * lhs, Vertex const * rhs) const
{
return *lhs < *rhs; // or whatever
}

};

std::make_heap(vertices.begin(), vertices.end(), less_vector_pointer());
Σnigma

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
Quote:
Original post by jyk
You could try creating a global (free) operator<() function that takes two Vertex pointers as arguments (I'm pretty sure this would work).
Overloaded operators must have at least one parameter of class type, reference to class type, enumeration type or reference to enumeration type.
Yup, wasn't thinking.

Share this post


Link to post
Share on other sites
I got so frustrated with operator overloading that I gave up on it and started overloading functions instead, e.g

vector multiply(vector,vector)

vector multiply(vector,matrix)

vector multiply(vector,scalar)

vector multiply(matrix,vector)

vector equals(vector,vector)

its much less headache for all concerned. I know its cowardly.

Share this post


Link to post
Share on other sites
Quote:
Original post by speciesUnknown
I got so frustrated with operator overloading that I gave up on it and started overloading functions instead, e.g

vector multiply(vector,vector)

vector multiply(vector,matrix)

vector multiply(vector,scalar)

vector multiply(matrix,vector)

vector equals(vector,vector)

its much less headache for all concerned. I know its cowardly.
Hm, that seems odd. Under normal circumstances (such as in a math library), operator overloading is pretty straightforward to implement. What sort of problems were you running into?

Personally, I'd much rather write:
matrix M = T*R*S;
Than:
matrix M = multiply(multiply(T,R),S);
In other words, I would argue that it's less of a headache for the user of the library if operator overloading is supported.

But that's just me :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this