You can type cast object in C++. The typecasted object will have only the members that are shared between the 2, so it will be typecast as the "lesser" of the 2.
I do this in my current game. I have 3 similar objects (they're all actually derived from a single base class, but for simplicity here we'll treat them as wholly unique; still works the same).
Object 1 - CTrooper
Object 2 - CBot
Object 3 - CWall
All 3 classes have these members in common:
[code]
//variables
RECT rcSrc;
LPDIRECTDRAWSURFACE7 sfSurface;
POINT ptBlt;
//functions
void Clip(RECT rcViewable);
void Blt(LPDIRECTDRAWSURFACE7 sfDest);
[code]
CTrooper adds a bunch of variables and functions to allow for adjust health, speed, firing accuracy, courage, getting input, etc.
Code:
//some examples of CTrooper's unique members
int iCourage;
void AdjustCourage(int iAdjust);
int GetInput(long lBtn, long lLastBtn, POINT ptMouse);
CBot adds a bunch of variables and functions to allow for determining how "smart" it is, get firing accuracy, determining actions base on AI, etc.
Code:
//a few examples
int iIntelligence;
int GetAction(int iIncomingCommands);
CWall only has a few additional items like wall "health" etc (the game will allow for misplaced shots to damage terrain, etc.
Code:
//ditto
int iDamage;
void UpdateSrcForDamage();
I have an array of pointers to the lowest level object, in this case CWall. So, if I have a total of 3 of these objects in the game (that would be one of each) then I have an array of 3 CWall pointers.
Code:
CTrooper ctrprSam;
CBot cbotFred;
CWall cwallWall;
CWall *pcwall[3];
In my blitting code I first copy the addresses of each of my original objects into the array of CWall pointers.
Code:
pcwall[0]=&ctrprSam;
pcwall[1]=&cbotFred;
pcwall[2]=&cwallWall;
Then I loop through the array of CWall pointers and sort them based on their ptBlt.y value (the higher up the screen they are the sooner they should be blitted, so they don't cover up someone/thing who's in front of them), and then run the Blit routine.
Code:
pcwallWall->Blt(sfBack);
Here's a more simple, generic example using structs (same principles apply):
Code:
struct num1
{
int i;
long l;
char c;
};
struct num2
{
int i;
long l;
};
num1 nm1;
num2 nm2;
num2 *pnm2;
nm1.i=10;
nm1.l=3600001;
nm1.c='a';
pnm2=&nm1;
cout<<pnm2->i<<endl; //would output 10
cout<<pnm2->l<<endl; //would output 3600001