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.

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

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.