Create generic classes to be specified later?

This is a discussion on Create generic classes to be specified later? within the C++ Programming forums, part of the General Programming Boards category; I have a list of different classes that need to be created, but I won't know which classes or how ...

Create generic classes to be specified later?

I have a list of different classes that need to be created, but I won't know which classes or how many of each are in my list. So I was wondering if I could create an array of generic classes who's specific class could be defined later? Is that possible, or is there any other sort of workaround? Any suggestions pertaining to this is welcome, thanks.

Ok, well what I have right now is a bunch of classes already written, defined, all of them being child classes off of my main generic sprite class.
Now, I'm reading in a file that has data that I parse and determine which of these classes that need to be initialized, and how many of each, but I'm not sure how I can initialize only the classes that I need, and only the amount that I need. It's sort of confusing, I hope I've clarified at least a bit. Thanks for the replies.

It sounds to me as if you want some kind of object factory. Have a look at the Factory Design Pattern and the Abstract Factory - this may give you some ideas (there are alot of variations on object factories.. some simple, some not so)

While googling my problem I did come across the term Abstract Factory, so I'll go look those up and see if it's what I need.

MacGyver:
My problem is sort of hard to explain... but what I want to do is this:
Read file. Parse data. Create generic objects. Set objects' classes to the ones specified in file. Set accompanying data to the classes.

I'm trying to work over, in design, your simpler plan and see if I can get it to work. It seems like it should work but I'm coming across the problems of what containers to use, how to ONLY create the arrays I need, how to do this efficiently without loads of cases for a switch statement, how to know which arrays I actually did create in the end, etc.
I don't know why this is stumping me, because it seems a trivial problem. But I think the problem I'm trying to describe is what wikipedia says for the Factory method pattern as "the problem of creating objects (products) without specifying the exact class of object that will be created."

There's no such thing as a "generic" object. you need to know the type of the object at the same time as you create it, because the object has all kinds of type information attached to it, including the size it occupies in memory - the size of the object cannot change.

The Abstract factory can let you associate types with some identifier (such as a std::string or an int), then you can build concrete objects using the factory based on the type identifier which you read from your file. You can then store base-class pointers to these concrete objects in some sort of container, such as a std::vector.

Yeah, the Abstract Factory Method sounds like the ticket.
What I have are three arrays. Two integer arrays, and one std::string array. The two integer arrays hold the x and y coordinates, and the string array holds the string identifiers for which class needs to be initialized with which coordinates, that way I can just grab one index number which (when plugged into any of the three arrays) will provide me with that one sprite's data. Now my next step is to turn these three arrays into actual created sprite objects, and I have all their classes written, I just don't know how to go about this.
So this does sound like a case where the Abstract Factory Method would help me out to you?

It sounds like a factory would fit the bill. There are all sorts of different ones possible. There should be plenty of material around the web which show various ways of automating the process. I suggest looking around to get an idea of whats involved (usually there is a "dictionary" involved somewhere, where the class identifiers are mapped on to some functors which actually create the object)

Ok, I looked it up and I *think* I've implemented a very basic version.
However, I have one problem, when I try to access a member function of the sub-class that's been created through the factory, it says that the base class doesn't have that function.
Here's what I have:
SpriteFactory.h

Now, at the test->draw(); part that I get errors, I get the error:
error: 'class Sprite' has no member named 'draw'
but the draw member function IS in the subclass! It's not in the base Sprite class, but it's in the sub class!! So does anyone have any idea why this is happening?
Thanks for the replies getting me this far, any further help's also appreciated.