Jump to content
  • Advertisement
Sign in to follow this  
Winegums

casting iterators into pointers

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

Yo, I'm writing an interface class. each interface object (button, slider, text box, whatever) inherits from a base theUIElement object. i have an stl::list of these in my interface class. The idea is that they'd be iterated through then, depending on an enumerated int detailing their object type, be cast into the type. firstly, is this a reasonable approach? i'm not too worried if it's not the most efficient approach, but if it's going to be one that'll scale terribly and theres a better sollution I'm all ears. secondly, how do i actually cast the iterator? From what i can see C++ seems to think the iterator is a void pointer?

Share this post


Link to post
Share on other sites
Advertisement
If you list is a "std::list<UIElement*>", then you convert iterators to pointers like this:

typdef std::list<UIElement*> MyListType;
MyListType myList;
MyListType::iterator i = myList.begin();
if( i != myList.end() )
UIElement* p = *i;//simply dereference the iterator

Share this post


Link to post
Share on other sites
Quote:
Original post by Winegums
depending on an enumerated int detailing their object type, be cast into the type.

Ad-hoc RTTI? It buuurns ussss!

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by Winegums
depending on an enumerated int detailing their object type, be cast into the type.

Ad-hoc RTTI? It buuurns ussss!


know ye of a better way? :p

Share this post


Link to post
Share on other sites
Quote:
Original post by Winegums
know ye of a better way? :p


The good old built-in C++ RTTI, as provided by your compiler.

Share this post


Link to post
Share on other sites
Hello

If you are writing an interface, why not simply have a pure method such as: -

virtual GUI_OBJECT_DETAILS GetObjectDetails() = 0;

Have this method implemented in each of your GUI objects and fill in the struct. That way you can keep to nice simple OO.

Share this post


Link to post
Share on other sites
Quote:
Original post by Winegums
know ye of a better way? :p

Or even better, rethink your design. Do you really need to know what type each pointer-to-parent really points to? Can you use polymorphism to do the same thing? edit: (as suggested above)

Share this post


Link to post
Share on other sites
Sometimes, you just need dynamic-casting in a GUI environment—for instance, to handle drag-and-drop of a component on top of another in a reasonable fashion in C++, you don't have much choice except for dynamic_cast.

Share this post


Link to post
Share on other sites
if i were to use polymorphism, how would each object 'know' what it is? If I want to make them into a list i need to have them all inherit from a common base type don't I? And to iterate the list I'd have to use an instance of this base type.

I've seen examples of polymorphism using single instances of the object, but never lists.

Share this post


Link to post
Share on other sites
Quote:
Original post by Winegums
if i were to use polymorphism, how would each object 'know' what it is?


Dynamic binding automagically calls the correct function for the dynamic type of the object, regardless of its static type.

Quote:
If I want to make them into a list i need to have them all inherit from a common base type don't I?


Exactly: you would instantiate an std::list<Base*> (perhaps with smart pointers) and fill it with derived class instances.

Quote:
And to iterate the list I'd have to use an instance of this base type.


No—to iterate through containers, one uses iterators which, as the name implies, are made for iterating.

Quote:
I've seen examples of polymorphism using single instances of the object, but never lists.


A list contains single instances. So, it's a simple matter of simply going through each instance in the list, and working polymorphically on that instance.

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!