Jump to content
  • Advertisement
Sign in to follow this  
timaer

[solved]Can C language have template?

This topic is 4494 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'm using C to make a little game,with the project getting more huge,I found something I can't use C to implement. I need the function could take a parameter,which can be any type,including struct type.But I found I could't let the void* pointer take the struct type. Can any guys tell me how can I do to realize the feature such like template in C++? Thanks in advance. [Edited by - timaer on March 27, 2006 7:01:06 AM]

Share this post


Link to post
Share on other sites
Advertisement
In C your best bet is to use macros. If you want templates you will have to switch to C++. Is there any reason why you are using plain C?

Share this post


Link to post
Share on other sites
Hey bud,

All i can suggest is that you just go ahead and write out a function for each of the types. This is what C++ replaces the need to do, with templates.

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
thanks Tjaalie and twanvl,could you give more detail about how to implement the function with macros?

and Hi,Dave,your idea is not bad ,if so,I have so many C function to be modified just becouse I need the feature which template can provide.I really hope I could rebuild my project and use C++ to do so,but I'm in a hurry to work the game out ,so I've to do it with C ;(

Share this post


Link to post
Share on other sites
EG:



#define TEMPLATE_COMPLEX_DECLARE( type ) \
typedef struct{ \
type real; \
type imaginary; \
}Complex##type;

TEMPLATE_COMPLEX_DECLARE( float );
TEMPLATE_COMPLEX_DECLARE( double );

int main( int argc, char **argv )
{
Complexdouble d;
d.real = 0.0f;
Complexfloat f;
f.imaginary = 1.0f;




[EDIT] arrgh, the source/code tags keep eating my "\" slashes...

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
EG:

*** Source Snippet Removed ***


That's horrid :P

Dave

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
Quote:
Original post by rip-off
EG:

*** Source Snippet Removed ***


That's horrid :P

Dave


Thats templates in c :P

c++ templates arent too much better to look at, mind you, until you're used to them

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
EG:

*** Source Snippet Removed ***

[EDIT] arrgh, the source/code tags keep eating my "\" slashes...



Hi,rip-off,you code looks nice,but still makes me a little confused(you know I'm bud;)

if I want to have a function like

int Animate_BOB(ANY_TYPE bob)

the ANY_TYPE could be monster or player struct type etc..

Could you give me the complete macro code for implementing this? ;)Thanks.

Share this post


Link to post
Share on other sites
Hmm, i suppose it would go like this:



#define DECLARE_ANIMATE_BOB( type ) \
int Animate_BOB##type( type bob );

#define DEFINE_ANIMATE_BOB( type ) \
int Animate_BOB##type( type bob ) \
{ \
do_stuff_with( bob ); \
return somevalue(); \
}

// So in a header file(s), you ca just say:

DECLARE_ANIMATE_BOB( Type1 );
DECLARE_ANIMATE_BOB( Type2 );

// And in the corresponding source file(s)

DEFINE_ANIMATE_BOB( Type1 );
DEFINE_ANIMATE_BOB( Type2 );




And you should be able to use the functions like so:

int result1 = Animate_BOBType1( bobType1 );
int result2 = Animate_BOBType2( bobType2 );

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!