I've been using this code style for a while now, I normally make a class that has "ID" in front of it, place it in a header, then make a .cpp file and put a class called "IDClassnameLocal". I make pure virtual functions in the abstract header class, then make normal virtual functions in the .cpp class and have it inherit the abstract header class.
Is this bad code design and am I coding efficiently?
- I put ID(identification) in front of the class name to prevent redefinition and for cleanness.
Example:
// Player.h // ////////////////////////////////////////////////////////////////
class IDPlayer {
public:
virtual ~IDPlayer(void) {} // Destructor
virtual void PlayerData(void) = 0;
virtual void Controls(void) = 0;
};
extern IDPlayer* idplayer;
// Player.cpp // //////////////////////////////////////////////////////////////
class IDPlayerLocal : public IDPlayer {
public:
IDPlayer(void) {} // Constructor
virtual void PlayerData(void);
virtual void Controls(void);
};
IDPlayerLocal idplayerLocal;
IDPlayer* idplayer = &idplayerLocal;
// Class Function Definitions
void IDPlayerLocal::PlayerData(void) {
Player.X = 400;
Player.Y = 500;
Player.W = 20;
Player.H = 20;
Player.VelocityX = (float)0.31;
Player.VelocityY = (float)0.31;
}
void IDPlayerLocal::Controls(void) {
if(::MainGame) {
if(KEY_DOWN(0x41)) { // A
Player.X = Player.X - Player.VelocityX;
if(Player.X <= 0)
Player.X = 0;
}
if(KEY_DOWN(0x44)) { // D
Player.X = Player.X + Player.VelocityX;
if(Player.X+Player.W >= 650)
Player.X = 650 - Player.W;
}
if(KEY_DOWN(0x57)) { // W
Player.Y = Player.Y - Player.VelocityY;
if(Player.Y <= 0)
Player.Y = 0;
}
if(KEY_DOWN(0x53)) { // S
Player.Y = Player.Y + Player.VelocityY;
if(Player.Y+Player.H >= 570)
Player.Y = 570 - Player.H;
}
if(KEY_DOWN(VK_SPACE)) {
}
}
}
// Core.cpp // ////////////////////////////////////////////////////////////////
// ...
// Intialized Data //
idplayer->PlayerData();
while(TRUE) {
// ...
// Loop Data //
idplayer->Controls();
// ...
}
// ...