Sign in to follow this  

casting iterators into pointers

This topic is 3717 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
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
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
Quote:
Original post by Winegums
if i were to use polymorphism, how would each object 'know' what it is?


It just does...

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?


Yes, but you were going to do that *anyway*, right?

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


No, to iterate over the list, you use an iterator over the list. Dereferencing the iterator gives you an element of the list, which would be hopefully a smart pointer (such as boost::shared_ptr) to a Base instance (though a raw pointer would work, if you're careful enough with the memory management - but why?) Then you use that to acheive polymorphism just as if it were a local-variable pointer to Base.

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


The list is merely where the pointers-to-instances happen to reside. That has no effect on how you make the polymorphism work.

Seriously; you're overthinking this.

Anyway, this kind of situation is exactly where polymorphism is at its most useful; when you write something like 'Base* b = new Derived(x, y); b->wibble();', you already know that it points at a Derived object - to get usefulness out of it, you need a situation in which there's enough time to "forget" (more practically, introduce uncertainty into) the object type. A list of user-requested objects is exactly that situation - you don't know what types he'll request.

Share this post


Link to post
Share on other sites

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this