Sign in to follow this  
MajinMusashi

Getting the type of objects being pointed in C++

Recommended Posts

CONTEXT: In my GUI library, the component's class hierarchy is defined like this (simplified for the example): COMPONENT (the base/root class) <- GRAPHICAL <- FOCUSABLE <- CONTAINER Container components contain one vector of components (actually pointers to components) for each of those types. I've made this way so the GUI don't need to search for ALL the components in a form, test if it is, for example, focusable, and finally perform a "onKeyPress" event on it (it just traverses the "focusables" vector). Currently, when a component is added to a container, the variable "Component::baseType" is used to decide to what vector(s) that component will be added. For example, a "panel" (baseType == container) will be added to all the vectors. PROBLEM: I think this approach is somewhat inelegant and prone to errors. I know of the "dynamic_cast" approach to solve this question, and also of using virtual methods to return the base type, but I don't really know what is the prefered way of doing this sort of work. QUESTION: Do you guys have any advice on how I should proceed? Is the general design decision I've made that bad?

Share this post


Link to post
Share on other sites
The way I'm understanding is that you have a form (window) that has a container. In this container, depending on the type of the object, you add it to different lists in order to minimise the time spent looking through the components for the window to send the right message to the right component.

Quick question: are you really going to have that many components on a single window to warrant seperating them? Because I can't imagine that the speed up would be worth the increased memory footprint (even though the memory footprint might well be trivial). If I'm understanding everything correctly, I don't know if it's worth it.

Share this post


Link to post
Share on other sites

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