Jump to content
  • Advertisement
Sign in to follow this  
graveyard filla

operator overloading w / inheritense and pointers

This topic is 5168 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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!