Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

t2sherm

Templates???

This topic is 6629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ô¿ô

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You don''t want templates. if Enemy and Gem derive from Sprite, then do:

[snippet]
static Sprite* apSprites[100];
statit const s_cSprites = sizeof(Sprite*)/sizeof(apSprites);

void Create
{
for ( int iSprite = 0 ; iSprite < s_cSprites ; iSprite++ )
{
if ( iSprite < s_cSprites/2 )
apSprites[iSprite] = new Enemy;
else
apSprites[iSprite] = new Gem;
}
}

void Draw()
{
for ( int iSprite = 0 ; iSprite < s_cSprites ; iSprite++ )
{
apSprites[iSprite]->Draw();
}
}
[/snippet]

This will create 50 enemies and 50 gems, and draw them all from one loop.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ARGH, how do you post code snippets here????

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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()

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!