Sign in to follow this  

operator overloading w / inheritense and pointers

This topic is 4865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);


Share this post


Link to post
Share on other sites
Nitpick: std::list<>'s member sort doesn't take any parameters. Use std::sort( foo_list.begin(), foo_list.end(), compare ) instead.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
thanks a lot Miserable!! this is exactly what i was looking for! (i was getting errors trying to do the global operator thing). psykr, it does work, thanks for your help though earlier =).

anyway, i just dont understand it... OK, so, instead of .sort() calling the < operator for my class, it calls the function i send it...

but, this function receives 2 pointers... so how does it work then? i mean, how does it know to sort my list by calling the function ? mainly, i mean, how does it know to send the function the actual object stored in the container, instead of the pointer to the object stored in the container (which in this case would be a pointer to a pointer) ? i dunno, sorry if that was hard to explain...

Share this post


Link to post
Share on other sites
*It doesn't*. You provide a function for it to pass the pointers to, instead of it just going to the default operator< (which for pointers, compares the addresses). The sort function passes pointers from the list to the comparison function. The comparison function *dereferences the pointers and compares the pointed-to values*.

Share this post


Link to post
Share on other sites
Quote:
Original post by psykr
Nitpick: std::list<>'s member sort doesn't take any parameters. Use std::sort( foo_list.begin(), foo_list.end(), compare ) instead.

You cannot do that. std::sort() requires its input iterators to be random access iterators, a requirement which std::list::iterator fails to meet. However, std::list provides two sort() member function - one that takes no parameters (and uses operator<()), and one that accepts a binary predicate, as examplified above.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
*It doesn't*. You provide a function for it to pass the pointers to, instead of it just going to the default operator< (which for pointers, compares the addresses). The sort function passes pointers from the list to the comparison function. The comparison function *dereferences the pointers and compares the pointed-to values*.


"The sort function passes pointers from the list to the comparison function."

ok, so the sort function passes WHATEVER is in the list to the functor? so if i had a regular list of Foo, and tried sending sort() the compare function, it wouldnt compile, since the list is of type Foo, but the compare function receives Foo*? just want to make sure i understand how it works. thanks again guys!

Share this post


Link to post
Share on other sites
Quote:
Original post by graveyard filla
"The sort function passes pointers from the list to the comparison function."

ok, so the sort function passes WHATEVER is in the list to the functor? so if i had a regular list of Foo, and tried sending sort() the compare function, it wouldnt compile, since the list is of type Foo, but the compare function receives Foo*?

Right.

Share this post


Link to post
Share on other sites

This topic is 4865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this