🔒 Locked
Templates???
Advertisement
Original Post
I have a class called Sprite. I have derrived 2 classes from it: Enemy and Gem. The base class Sprite has a draw function that i wrote, so to draw the sprites i use Sprite.Draw.
Here is my question:
To declare these things i have to use
SPRITE Sprite[10];
ENEMY Enemy[5];
GEM Gem[25];
Like that. But them i have to have 3 loops like this
for (int i =0;i<10;i++)
Sprite[i].Draw();
for (i =0;i<5;i++)
Enemy[i].Draw();
for (i =0;i<25;i++)
Gem[i].Draw();
But this kind of eliminates the whole idea of them all inheriting the base class draw. all of these things will call the same Draw function. I was reading some stuff about templates, but i don''t really understand it very much and i was wondering if it is possible with them or with anything in C++ to have something like this:
GENERALSPRITE GenSprite[40];
This would represent all of the classes, but some would be of different types.
Then just have 1 loop going from 0 to 39 calling GenSprite[i].Draw();
Is this possible???
Thanks.
t2sherm ô¿ô
I'm guessing you want pure virtual base classes. COM objects use interfaces, these can be implemented as classes with pure virtual member functions and no data.
Note that there are no data members and none of the functions have any implementation. Under the hood, the compiler creates a virtual function table (sometimes the debugger calls it "__lpvftbl" and you can see the __lpvftbl when you step through the code). Pure virtual base classes, or "Interfaces" denoted by the capital "I" prefix, have a virtual function table such that all the function pointers are null! (Note the explicit "= 0;")
Edited by - Marsupial Rodentia on June 27, 2000 1:36:05 PM
|
Note that there are no data members and none of the functions have any implementation. Under the hood, the compiler creates a virtual function table (sometimes the debugger calls it "__lpvftbl" and you can see the __lpvftbl when you step through the code). Pure virtual base classes, or "Interfaces" denoted by the capital "I" prefix, have a virtual function table such that all the function pointers are null! (Note the explicit "= 0;")
Edited by - Marsupial Rodentia on June 27, 2000 1:36:05 PM
The next step is to derive classes from the interface. And then override the virtual member functions to provide your specific implementation.
Now I can generically call (40 times if I so desire) pMyPureVirtualBase->MyFunc1() even though pMyPureVirtualBase points to an instance of CMySecondDerivitive and I will get the implementation of CMySecondDerivitive::MyFunc1()
|
Now I can generically call (40 times if I so desire) pMyPureVirtualBase->MyFunc1() even though pMyPureVirtualBase points to an instance of CMySecondDerivitive and I will get the implementation of CMySecondDerivitive::MyFunc1()
Some notes of caution when you are trying to compile:
You can not declare an instance of IMyPureVirtualBase nor can you create one with the new operator.
You will probably want to typecast when you create the object
And when debugging this code:
If your pointer pMyPureVirtualBase is not pointing to anything when you call the member functions, guess what...your program is likely to crash due to illeagal instructions (This may be the number one reason for the x86 instruction pointer to point to address 0x00000000.)
Edited by - Marsupial Rodentia on June 28, 2000 11:04:38 AM
You can not declare an instance of IMyPureVirtualBase nor can you create one with the new operator.
You will probably want to typecast when you create the object
|
And when debugging this code:
If your pointer pMyPureVirtualBase is not pointing to anything when you call the member functions, guess what...your program is likely to crash due to illeagal instructions (This may be the number one reason for the x86 instruction pointer to point to address 0x00000000.)
Edited by - Marsupial Rodentia on June 28, 2000 11:04:38 AM
Once, on a project, I had to learn the hard way. I wrote code similar to this
This code drove me nuts for hours and hours and hours. I tried setting break points and my debugger was jumping over the break points. I was getting so pissed off at myself.
I tried and tried. I tried different approaches. I set my data members.... Suddenly it hit me! Whenever you make virtual functions, the compiler makes __lpvftbl your first data member (first 32 bits pointed at by the C++ "this" keyword ). By using memset() I was zeroing out the pointer to the virtual function table.
|
This code drove me nuts for hours and hours and hours. I tried setting break points and my debugger was jumping over the break points. I was getting so pissed off at myself.
I tried and tried. I tried different approaches. I set my data members.... Suddenly it hit me! Whenever you make virtual functions, the compiler makes __lpvftbl your first data member (first 32 bits pointed at by the C++ "this" keyword ). By using memset() I was zeroing out the pointer to the virtual function table.
Sorry, I got carried away. Note that when you use the "[source]" tags you can only put one piece of source in each posting. I was going for interlaced code and text, and I was typing faster than I could think. Thanks for stopping me.
Edited by - Marsupial Rodentia on June 27, 2000 1:43:47 PM
Edited by - Marsupial Rodentia on June 27, 2000 1:43:47 PM
ooooooo! can you hear me cry out? we''re not worthy! we''re not worthy!
i never expressed nor implied anything about knowledge. i was just bowing down to amout of time you've spent (or wasted) reading these discussions.
Edited by - Marsupial Rodentia on June 27, 2000 2:27:54 PM
Edited by - Marsupial Rodentia on June 27, 2000 2:27:54 PM
HAHAHA. come on now. You can''t say you weren''t asking for it.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement