casting iterators into pointers

Started by
9 comments, last by Zahlman 16 years, 6 months ago
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?
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
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!
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
Quote:Original post by Winegums
know ye of a better way? :p


The good old built-in C++ RTTI, as provided by your compiler.
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.
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)
[size="1"]
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.
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.
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.

This topic is closed to new replies.

Advertisement