Skip to main content
gamedev.net
🔒 Locked

Templates???

Started by t2sherm Jun 27, 2000 at 9:59 AM 15 replies 650+ views
Original Post
t2sherm
t2sherm
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 ô¿ô
t2sherm ô¿ô
mossmoss
mossmoss
quote:
Original post by Anonymous Poster
ARGH, how do you post code snippets here????


    
[source]
place code here
[/source]



---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
Marsupial Rodentia
Marsupial Rodentia
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.
                    
class IMyPureVirtualBase
{
public:
virtual void MyFunc1() = 0;
virtual int MyFunc2( int nNum ) = 0;
virtual char MyFunc3( char* strName ) = 0;
};


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
Marsupial Rodentia
Marsupial Rodentia
The next step is to derive classes from the interface. And then override the virtual member functions to provide your specific implementation.
    
class CMyDerivedClass : public IMyPureVirtualBase
{
public:
void MyFunc1()
{
// Must declare the override even if the function is not implemented.

}
int MyFunc2( int nNum )
{
}
char MyFunc3( char* strName )
{
}
};

class CMySecondDerivitive : public IMyPureVirtualBase
{
public:
void MyFunc1()
{
// Note that I don''t have to use the virtual keyword here.

}
int MyFunc2( int nNum )
{
}
char MyFunc3( char* strName )
{
}
};


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()
Marsupial Rodentia
Marsupial Rodentia
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
            
// Typecast like this (the ANSI C style way)
IMyPureVirtualBase* pMyPureVirtualBase = (IMyPureVirtualBase)new CMySecondSerivitive();

// Or you can typecast like this (The 1998 ANSI/ISO C++ standard)
IMyPureVirtualBase* pMyPureVirtualBase = static_cast<IMyPureVirtualBase*>( new CMySecondSerivitive() );



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
Marsupial Rodentia
Marsupial Rodentia
Once, on a project, I had to learn the hard way. I wrote code similar to this
    
class CCanYouFindTheBug
{
// D A T A

private:
int m_nMyData;
int m_nMyData2;
int m_nMyOtherData;

// C O N S T R U C T I O N / D E S T R U C T I O N

public:
CCanYouFindTheBug()
{
memset( this, 0, sizeof(this) );
}
~CCanYouFindTheBug()
{
}

// M E T H O D S

public:
virtual void MyFunc1()
{
}
virtual void MyFunc1()
{
}
};

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.
Gladiator
Gladiator
what''s up with the multiple postings Mr.Zealot?
Marsupial Rodentia
Marsupial Rodentia
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
Gladiator
Gladiator
hehe

btw, I just became a Zealot when I posted to inform you of your multiple postings... How interesting... hmmm
Gladiator
Gladiator
That only means I''ve posted more messages than you... not that I''m more knowledgable... which might be possible
Marsupial Rodentia
Marsupial Rodentia
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
Gladiator
Gladiator
being a smartass with me? hmmm...
Gladiator
Gladiator
I wasn''t asking for anything to tell you the truth... I''m just playing with people

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.