Hi, there:
I'm looking for a little help in creating a framework for a program I'm puttering with. I don't do a lot of object-oriented programming in C++ (or really that much in general-- I'm an AI theory person :) ), and was hoping you guys might be able to help.
I have created a vector of a base class, and have populated it with various instances of (different) derived classes. The salient structure is fairly straightforward, namely a virtual method called runMe (for example) in the base class, and its implementation in the respective derived classes.
I'm creating a program whereby a menu-type listing of the objects in this vector can be produced, but the specifics of what each object is (i.e. which derive class) can remain hidden. Thus I
want something like the following:
1 2 3 4 5 6 7
|
...
vector<Base> things;
...
Base* thing = things[itemNum];
thing->SomeDerivedClass::runMe();
...
|
..and not...
1 2 3 4 5 6 7
|
...
vector<Base> things;
...
Derived* thing = static_cast<Derived*> (things[itemNum]);
thing->SomeDerivedClass::runMe();
...
|
While the code immediately above works and makes sense to me as to why it should be that way, I wonder if there is another way around this such that I could continue to hide the specifics of the derived classes?
I'm really wanting to have something where I can fill the vector (declared as type Base) with objects from derived classes in one function, and then create a very general function that simply calls the derived method based on the current object without having to cast from Base to Derived every time. Otherwise I'll end up having to write a chunk of tailored code for each vector object to perform the conversion and then call. It seems that if I were smart I should be able to find a way to maintain the flexibility I'm looking for, but I haven't yet been able to find a comparable problem on the web (though I'm sure they're out there).
(Just a bit of further background, in particular I'm trying to create a framework for my students to encapsulate the specifics... I just want them to have to design the derived classes (I'll have designed everything else), and be able to create as many as they like, without having to worry about the internals.)
Anyhow, thanks for any responses...
-z.