Overloading the less than operator
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
Quote:Original post by saeedmYou could try creating a global (free) operator<() function that takes two Vertex pointers as arguments (I'm pretty sure this would work).
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
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...)
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.
Hope this helps
template <class T>class Vertex {private: float distance; T x, y, z; // other members herepublic: 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
Quote:Original post by AtomThe OP is not storing Vertex objects, but rather pointers to Vertex objects.
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.
Quote:Original post by jykOverloaded operators must have at least one parameter of class type, reference to class type, enumeration type or reference to enumeration type.
You could try creating a global (free) operator<() function that takes two Vertex pointers as arguments (I'm pretty sure this would work).
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
Quote:Original post by EnigmaYup, wasn't thinking.Quote:Original post by jykOverloaded operators must have at least one parameter of class type, reference to class type, enumeration type or reference to enumeration type.
You could try creating a global (free) operator<() function that takes two Vertex pointers as arguments (I'm pretty sure this would work).
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.
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.
Quote:Original post by speciesUnknownHm, 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?
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.
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 :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement