Jump to content
  • Advertisement
Sign in to follow this  
serratemplar

STL containers, pointers, and custom classes.

This topic is 3044 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

There's something fundamental here that I am missing or don't recall. What I have is an stl list of pointers to a custom class Foo, built thus.


list<Foo*> list_of_foo;






I create several instances of Foo objects and push_back() the pointers into that list; I can iterate over them and examine them without event, though the code to dereference them is not the prettiest with a nested dereference.


list<Foo*>::iterator i = list_of_foo.begin();
(*i)->foo_member_function();






I seem to recall there being a way to pretty this up, either with templates or typedef hackery, but it escapes me. Now I think I may need it, as I've confronted a problem: the following yields an invalid type conversion error (focused on the cast)


Foo *tmp = (Foo*) list_of_foo.pop_front();






I seem to remember running into this trouble before, but looking over online docs regarding the STL container haven't run any bells for me, or even talked about this circumstance. I'm honestly confused as to even why I have to cast it at all as the container is supposed to hold a pointer to Foo: though I presume it's because I need to peel the shell of the Container away, reinterpret it, or perhaps the typing is lost and a cast is required. Either way, I'm trying to cast it now and it's no go. Can anybody help me understand what's going wrong here?

Thank you very much in advance.

EDIT: cleared up some typos which imposed inconsistencies on my examples.

Share this post


Link to post
Share on other sites
Advertisement
Well, the big problem is probably that std::list<>::pop_front() returns void. Trying to cast it to anything is a mistake.

Share this post


Link to post
Share on other sites
Quote:
The function pop_front() removes the first element of the list.

Took from the C++ reference documentation, I think you want to get the front value of the list, for such purpose you need to use the "front()"; gets the first value inserted or "back()"; gets the last inserted.

Share this post


Link to post
Share on other sites
OOOOOOH of course!! Dammit.

pop_front doesn't return the front value, it just destroys it. I completely forgot. Of course it returns void. >_<

Fundamental, indeed. Thank you, SiCrane.

EDIT: Pretty weird typo. It's really not my day. <_<

[Edited by - serratemplar on July 20, 2010 6:50:18 PM]

Share this post


Link to post
Share on other sites
Quote:
I seem to recall there being a way to pretty this up, either with templates or typedef hackery


If you are writing a loop for some kind of custom algorithm, and using 'i' several times, you can use a reference:

Foo& current = **i;
current.wibble();
current.wobble();


Of course, you still get undefined behaviour if there are nulls in the list.

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!