By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,563 Members | 1,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,563 IT Pros & Developers. It's quick & easy.

std::less and pointers?

P: n/a
Hello

How can I define an operator for such:

bool operator<(my_class *l,my_class *r)
{
return l->_attribute < r->_attribute;
}

So that I can use with std::less, as:

std::set<my_class *, std::less<my_class*> > set1_t;

In VC++ compiler I get this error:
error C2803: 'operator <' must have at least one formal parameter of class
type.

It needs me to define the operator as:

bool operator<(my_class &l,my_class &r)

(It require me not to use pointers)

Please advise.

There is another solution is to write the comparator class and overload the
() operator.

Any other solutions?

--
Elias
Mar 28 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi

lallous wrote:
How can I define an operator for such:

bool operator<(my_class *l,my_class *r)
You cannot overload operators for non-user-defined types.
{
return l->_attribute < r->_attribute;
}
And this wouldn't work properly anyway. The comparisons for pointers are
only defined when both pointers point to the same object, or to objects
from the same array.

To compare two distinct and unrelated pointers, you _must_ use std::less.
So that I can use with std::less, as:

std::set<my_class *, std::less<my_class*> > set1_t;
Why don't you just give a different comparison functor?
In VC++ compiler I get this error:
error C2803: 'operator <' must have at least one formal parameter of class
type.
Your compiler is correct.
It needs me to define the operator as:

bool operator<(my_class &l,my_class &r)
Which is a completely different thing...
There is another solution is to write the comparator class and overload
the () operator.
It's the natural and correct way.
Any other solutions?


I don't think so.
Why do you need one?

Markus
Mar 28 '06 #2

P: n/a
Markus Moll wrote:
return l->_attribute < r->_attribute;
}


And this wouldn't work properly anyway. The comparisons for pointers are
only defined when both pointers point to the same object, or to objects
from the same array.


Presumably _attribute is NOT a pointer, but some type where
< is well defined between different objects.
Mar 28 '06 #3

P: n/a
Hi

Ron Natalie wrote:
Presumably _attribute is NOT a pointer, but some type where
< is well defined between different objects.


D'oh... I missed that part... :-/ sorry for the confusion.

Markus
Mar 28 '06 #4

P: n/a
If you're trying to do this because you want to use your objects in a
sorted container, or you want to sort a container, then consider using
a smart pointer that has value semantics for the comparison operators.
Check out the following smart pointer:
http://axter.com/smartptr

You can use the above smart pointer in a sorted container, without
having to create an operator for the pointers.
std::set<smart_ptr<my_class> > myset;
std::map<smart_ptr<my_class>, int > mymap;

----------------------------------------------------------------------------------------
David Maisonave
http://axter.com

Top ten member of C++ Expert Exchange:
http://www.experts-exchange.com/Cplusplus
----------------------------------------------------------------------------------------

Mar 29 '06 #5

P: n/a
Hello Markus

"Markus Moll" <mo**@rbg.informatik.tu-darmstadt.de> wrote in message
news:ne********************@luna.igd.fhg.de...
Hi

lallous wrote:
How can I define an operator for such:

bool operator<(my_class *l,my_class *r)
You cannot overload operators for non-user-defined types.


So if I typedef my_class * myclass_p , then write the operator using
myclass_p then the compiler won't complain?
(I defined my user type)

And this wouldn't work properly anyway. The comparisons for pointers are
only defined when both pointers point to the same object, or to objects
from the same array.

To compare two distinct and unrelated pointers, you _must_ use std::less.


Sorry how do you mean it?

So that I can use with std::less, as:

std::set<my_class *, std::less<my_class*> > set1_t;


Why don't you just give a different comparison functor?

There is another solution is to write the comparator class and overload
the () operator.


It's the natural and correct way.
Any other solutions?


I don't think so.
Why do you need one?


I didn't want to write another class, instead I just wanted to overload an
operator and use std::less

--
Elias
Apr 3 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.