Well, I have to say that I thought about polymorphism.
And what result did you arrive at?
Maybe I will
explain exactly why do I need such a variable. I'm working on a program
which is a MFC Application. I didn't choose this technique and didn't
have any opportunity to influence this choice.
Now, I have this nice window which is containing several Dialogs. So I
created a class which contains the pointer to a Dialog. I store all
those classes in a vector. Sometimes I need to reposition or do
anything with, for instance, 'dialog no 3' and I need a handle to this
dialog. I call 'my_vector[3]->window' and I have Dialog handle which is
moreless exactly the same as the rest of Dialog handles (differences
are non-relevant to me) but it represents different data type.
Why do you say that it represents a different data type? It's a handle
to some OS-specific structure. How do you use it? You send a message
to that window using OS-specific method, no?
In MFC and many other UI frameworks, dialogs are windows, and classes that
define (describe, represent) dialogs usually derive from a common base
class that defines (describes, represents) a window. That way, if you
need to do something window-specific, you just tell the object to do that
using a member of the window class. Like reposition, show/hide, resize,
refresh, et cetera.
So, you have a main window that does something to those dialogs. What
does it do to them? Is it the same operation for all of them? Is it
different for each of them? If it's the same set of operations, collect
those operations in a class and make that class common base class for
all your 'dialog' classes. If it's different for each 'dialog' type,
what business do they have being in the same window? See my point?
An example of a common base class in MFC is CToolBar. You can either
instantiate it (it's a concrete class) or you could derive from it and
make your own, customized "toolbar". Then you can have both "native"
toolbars and your new, customized, one in the same main frame window.
All the operations you can perform on a regular toolbar is OK for your
custom one too because it "is-a" CToolBar, it derives from one.
I don't want to continue this discussion here because it's not really
relevant to C++. It's all essentially about knowing your library (MFC
in your example) well and designing your classes correctly using that
framework. You definitely need more time on that.
First of all, figure out what is common between those "different" data
types. Perhaps not that little. If they have a common class, store
a pointer to that. Use those pointers because that's the only way you
will get to the common functionality of those classes. If you can't
_find_ anything in common between them, you probably didn't put it in,
since there _must_ be something in common. So, revisit the hierarchy
of classes you have, and _give_ all those "different data types" the
common base class that will contain the functionality they all need to
implement.
V