As an assignment, I have been ordered to program the game Breakout/Arkanoid. We have to implement a class hierarchy starting with an Entity. We then expand this class to a PlayerEntity and an EnemyEntity. Then PlayerEntity is expanded to a PlayerBall and PlayerPaddle. EnemyEntity is expanded to EnemyBlock and EnemyProjectile. These last four classes are all expanded to have a graphical implementation with SDL.

Here is where my problem starts. I want to have a function that traverses a list of Entity's.(This can be the basic class, but more likely will be a collection of all the expanded classes) For every node in the list, I would like to call a member function of each Entity, called displayEntity(). Here is the problem. I have to give this function a parameter, but this parameter relates to SDL. This means that my basic class, Entity, should not have any knowledge of this parameter, since it should be implementation-independent. The only way I see this is work is when I make this function virtual. But in order to do that, I have to declare this function as virtual in Entity, and the parameter is needed.

I'm aware that this may be very confusing to read. I've done my best to explain it. English is not my native language, and I apologize for any mistakes I made. If you need more information, just post and ask me.

Thanks a lot for reading, and thanks even more if you could take the time to answer.

Regards

Pieter

Recommended Answers

All 2 Replies

If you pay attention to this sentence:
>>These last four classes are all expanded to have a graphical implementation with SDL.
You see that the last four classes should be expanded to include some display function (and maybe others). This calls for multiple inheritance. You can make another base class which is dependent on SDL and has this pure virtual "display" function with whatever parameters you need. Now, your last four classes should inherit from their respective base class AND from this SDL visible object class.

This way, all the code that uses the other base classes (Entity, PlayerEntity, etc.) doesn't need to have any dependence on SDL. And your code that handles the rendering and the SDL window can simply keep a record of all SDL visible objects (as base classes) and call their virtual methods for displaying them.

Thanks Mike. Your suggestion solved my problem. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.