The thing with every object storing an image is that it would add to the size of each object, |
Unless they didn't store the actual duplicated image over and over. You are already creating these actual images somewhere, yes? So all each object needs to do is return a pointer or reference to that image.
You could, for example, have a function somewhere:
1 2 3 4 5 6 7 8
|
image* getImageFromEnum(enum which_image)
{
if (which_image == STANDARD_IMAGE)
{
return a_pointer_to_the_standard_image;
}
else if ( ... etc etc ... )
}
|
Your objects would then have a base function getImage of form:
1 2 3 4
|
image* getImage()
{
return getImageFromEnum(ENUM_VALUE_HARD_CODED_IN_THIS_DERIVED_TYPE)
}
|
where ENUM_VALUE_HARD_CODED_IN_THIS_DERIVED_TYPE is different for each derived type.
Or even just don't bother with enums and so on; if each type will always need to return the same image, you don't need enums to work out what type of object you're dealing with - have the derived class just return the correct image for that class.
Another option; if you made the image itself a static member variable of each derived class, then there would only ever be ONE instance of it in memory, shared by all the instances of the classes. The language wants to work with you; you just have to let it! This does come with time; learning C++ syntax is only the start, and knowing the syntax is not the same an knowing how to think in C++, how to work with the flow of the language.
The images wouldn't have to exist at the time the objects are created. They're only needed when you have to actually draw something on the screen.
I get the feeling you're still thinking quite low-level; you're doing for yourself various administrative things that C++ wants to do for you. Like giving each derived type an enum so you can tell what type it is; no, the compiler and the running program should know about that and should call the right function for you, without you having to manually direct things - this is what inheritance is for. If you do it all yourself, then don't bother with inheritance, since you're not making any use of what inheritance offers - just have one kind of object.