operator overloading w / inheritense and pointers

Started by
13 comments, last by Miserable 19 years, 8 months ago
hi. this is actually 2 seperate questions. if i define the < operator for class Foo, then make a list of Pointers to Foo, and try to sort it, what will happen exactly? for exmaple:

std::list<Foo*> foo_list;

foo_list.push_back(new Foo());
foo_list.push_back(new Foo());

(do stuff with them)

foo_list.sort();
what happends here? im gunna take a guess that it will call the < operater for 2 integers, and sort the pointers by their pointer value, huh ? how do i make it instead call the < operator for the objects stored in the pointer? the second question is, are operators inherited? can i make them pure interface inheritense? i want to make a parent class and have it define the < operator, so that children can inherit from it and make their own < operator, then i store a list of pointers to the parent class, and .sort() it, and it will hopefully sort using the actual children's < operator. is this possible? thanks for any help!
FTA, my 2D futuristic action MMORPG
Advertisement
Quote:Original post by graveyard filla
hi. this is actually 2 seperate questions.

1) if i define the < operator for class Foo, then do this:

std::list<Foo*> foo_list;foo_list.push_back(new Foo());foo_list.push_back(new Foo());(do stuff with them)foo_list.sort();


what happends here? im gunna take a guess that it will call the < operater for 2 integers, and sort the pointers by their pointer value, huh ? how do i make it instead call the < operator for the objects stored in the pointer?


Yes it will only sort by addressess, you would need to use the generic version of sort in algorithm header and give either a free-function, functor, or member function that will do the comparions correctly.

Quote:Original post by graveyard filla
the second question is, are operators inherited? can i make them pure interface inheritense? i want to make a parent class and have it define the < operator, so that children can inherit from it and make their own < operator, then i store a list of pointers to the parent class, and .sort() it, and it will hopefully sort using the actual children's < operator.


You can make virtual operators but i wouldn't you wont get anything useful in doing such a thing. Besides it will still compare addressess.
Forget about the first bit i said because you can only use the generic sort algorithms with random access iterators which list doesn't have, you can only use list's member function sort but storing raw pointers it will only sort by addressess in short only sorting pointers.
so theres no way for me to sort the pointers? hmm.... i have no idea on how to implement this then.. im working on a 2d RPG and want to sort my characters for drawing. i make a ABC called Render_Object. then Player, Enemy, and NPC inherit from it. next i keep a master std::list of Render_Object*. each frame, i want to sort my list then render everything in it. hmm... couldnt i make a global < operator which took 2 pointers to a Render_Object? what about this?

bool operator < (const Render_Object *r1, const Render_Object *r2){   return r1->Get_Y() < r2->Get_Y();}


will this work? then i just over-ride the Get_Y() function for NPC, player, and Enemy. since their x/y Position will be stored independant of the Render Object classs.
FTA, my 2D futuristic action MMORPG
My suggestion would be to create a sort of smart pointer class. Then you can overload the comparison operators to do whatever you want.
Plus, it will create a lot less headaches with memory managment!
well, im not worried about memory management as the pointers im dealing with are already being managed by my engine. so will my system work, or not ?
FTA, my 2D futuristic action MMORPG
Quote:Original post by graveyard filla
well, im not worried about memory management as the pointers im dealing with are already being managed by my engine. so will my system work, or not ?


Global operators are the best bet.
Not giving is not stealing.
Quote:Original post by thedevdan
Quote:Original post by graveyard filla
well, im not worried about memory management as the pointers im dealing with are already being managed by my engine. so will my system work, or not ?


Global operators are the best bet.

You cannot overload operators, global or not, to take two operands of pointer type.

What the OP needs is a function or functor to pass to the sorting function that dereferences and compares the objects, e.g.

class Foo{    // ...};bool compare(Foo* f1, Foo* f2){    return *f1 < *f2;}//...std::list<Foo*> foo_list;foo_list.push_back(new Foo());foo_list.push_back(new Foo());foo_list.sort(compare);
Nitpick: std::list<>'s member sort doesn't take any parameters. Use std::sort( foo_list.begin(), foo_list.end(), compare ) instead.
Quote:Original post by Miserable
Quote:Original post by thedevdan
Quote:Original post by graveyard filla
well, im not worried about memory management as the pointers im dealing with are already being managed by my engine. so will my system work, or not ?


Global operators are the best bet.

You cannot overload operators, global or not, to take two operands of pointer type.
...

[embarrass]
I'm humiliated.
[bawling]
Not giving is not stealing.

This topic is closed to new replies.

Advertisement