std::less and pointers?

L

lallous

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?
 
M

Markus Moll

Hi
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
 
R

Ron Natalie

Markus said:
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.
 
M

Markus Moll

Hi

Ron said:
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
 
A

Axter

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
----------------------------------------------------------------------------------------
 
L

lallous

Hello Markus

Markus Moll said:
Hi


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?

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


It's the natural and correct way.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top