473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Operator< overloading

Hello,

i am having trouble to use the sort() function of a list<>.

I cannot seem to overload the operator<().
The variables to compare are references, like

Item* itemA;
Item* itemB;

when comparing "itemA < itemB" my overloaded function isnt called. When
the variables are defined;

Item itemA;
Item itemB;

the overloaded function is called.

I hope it is possible for what i want.
Below is my "operator<", also a "const diritem& rhs" didnt resolve my
problem.
int diritem::operator<(const diritem* rhs) const {

printf("%s < %s\n",this->_filename.c_str(), rhs->_filename.c_str());

if ((this->_type==DT_DIR) && (rhs->_type!=DT_DIR)) {
return 1;
}
if ((this->_type!=DT_DIR) && (rhs->_type==DT_DIR)) {
return 0;
}

return (this->_filename < rhs->_filename);

}

Any pointers are welcome!

Kind regards,
Bas Nedermeijer
Aug 16 '06 #1
7 5104

If you make the operator argument "const &" again, you should be
able to call the operator with;

*ptr < *ptr

in case of pointers.

Hope this helps.

Tolga Ceylan

Aug 16 '06 #2
to***********@yahoo.com wrote:
If you make the operator argument "const &" again, you should be
able to call the operator with;

*ptr < *ptr

in case of pointers.
I cannot change the sorting routine, because i want to make use of the
built-in sorting routing of list<>.

But thanks for the reply,

Kind regards,
Bas Nedermeijer
Aug 16 '06 #3
Bas Nedermeijer a écrit :
to***********@yahoo.com wrote:
>If you make the operator argument "const &" again, you should be
able to call the operator with;

*ptr < *ptr

in case of pointers.

I cannot change the sorting routine, because i want to make use of the
built-in sorting routing of list<>.

But thanks for the reply,

Kind regards,
Bas Nedermeijer
Hi ! Your problem is that you cannot overload the < operator for
pointers, as it already exists (it compare the actual address of the
pointers). You need either to create a list of objects (and not
pointers), or to use smart pointers. In the case of smart pointers, you
will be able to redefine the < operator for them. However, why do you
use lists of pointers ? This is very dangerous as the list will never
take care of freeing memory before removing elements.

Pierre
Aug 16 '06 #4
Bas Nedermeijer wrote:
Hello,

i am having trouble to use the sort() function of a list<>.

I cannot seem to overload the operator<().
The variables to compare are references, like

Item* itemA;
Item* itemB;

when comparing "itemA < itemB" my overloaded function isnt called. When
the variables are defined;

Item itemA;
Item itemB;

the overloaded function is called.

I hope it is possible for what i want.
Below is my "operator<", also a "const diritem& rhs" didnt resolve my
problem.
int diritem::operator<(const diritem* rhs) const {

printf("%s < %s\n",this->_filename.c_str(), rhs->_filename.c_str());

if ((this->_type==DT_DIR) && (rhs->_type!=DT_DIR)) {
return 1;
}
if ((this->_type!=DT_DIR) && (rhs->_type==DT_DIR)) {
return 0;
}

return (this->_filename < rhs->_filename);

}

Any pointers are welcome!

Kind regards,
Bas Nedermeijer
The list container has a sort member template that takes a binary
predicate to use for sorting. What you need to do is pass in a
predicate that sorts the objects based on pointers to those objects.
The following template may help:

template <typename T>
struct ptr_less
{
bool operator()(T * p1, T * p2)
{
return *p1 < *p2 ;
}
} ;
And, for what it is worth, here is the program I used to test that
template:

template <typename T>
struct ptr_less
{
bool operator()(T * p1, T * p2)
{
return *p1 < *p2 ;
}
} ;

#include <list>
#include <iostream>
#include <algorithm>

void ptr_out(int * p)
{
std::cout << *p << ' ' ;
}

int main()
{
int a[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 } ;

// Add pointers to the list.
std::list<int *v ;
for (std::size_t i = 0; i < sizeof(a)/sizeof(a[0]); ++i)
v.push_back(a+i) ;

// Show the list unsorted.
std::for_each(v.begin(), v.end(), ptr_out) ;
std::cout << std::endl ;

// Sort the list.
v.sort(ptr_less<int>()) ;

// Show the list sorted.
std::for_each(v.begin(), v.end(), ptr_out) ;
std::cout << std::endl ;
}

--
Alan Johnson

Aug 16 '06 #5

"Pierre Barbier de Reuille" <p.****************@free.frwrote in message
news:44e36a71>
However, why do you use lists of pointers ? This is very dangerous as the
list will never take care of freeing memory before removing elements.
I can't answer for the OP, but a quite common reason to store pointers is
when you want polymorphism. Storing base class pointers allows you to store
descendant types in the container.

-Howard
Aug 16 '06 #6
Bas Nedermeijer wrote:
to***********@yahoo.com wrote:
>If you make the operator argument "const &" again, you should be
able to call the operator with;

*ptr < *ptr

in case of pointers.

I cannot change the sorting routine, because i want to make use of the
built-in sorting routing of list<>.
No problem: just define a custom comparison function

bool compare_item_ptr ( item*, item* ) {
// whatever
}

and do

list<item*l;

...

l.sort( compare_item_ptr );
Best

Kai-Uwe Bux
Aug 16 '06 #7
Alan Johnson wrote:
Bas Nedermeijer wrote:
<snip>

The list container has a sort member template that takes a binary
predicate to use for sorting. What you need to do is pass in a
predicate that sorts the objects based on pointers to those objects.
The following template may help:
<snip>

Thanks for your solution, it works!

- Bas Nedermeijer
Aug 17 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Victor | last post by:
Anyone knows how to write a virtual function for operator<< ? I have a base class and some public derived class from it. For the derived class, I hope they can use << to output their different...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
3
by: Robert Wierschke | last post by:
Hi I want to overload the operator<< for a class Vector. class Vector { double x; double y; double z;
5
by: Ian Lazarus | last post by:
Hello, My question is whether it is possible to avoid assignment on the left hand side of an overloaded operator << expression, as in the code below. Without the assignment, the compiler...
25
by: Steve Richter | last post by:
is it possible to overload the << operator in c# similar to the way it is done in c++ ? MyTable table = new MyTable( ) ; MyTableRow row = new MyTableRow( ) ; row << new MyTableCell( "cell1 text...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
5
by: TOMERDR | last post by:
Hi, I was requested to write a code similar to this: CArchive ar; //Not mfc CArchive..... .... void * vp = &XXX; // any object. ar << vp, sizeof(XXX);
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
2
by: Adam Nielsen | last post by:
Hi everyone, Following advice previously given in this group, I've created a function that I'm using to "inline" the creation of a string. Regardless of what you think of my method, I'm...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.