[solved]Can C language have template?

Started by
21 comments, last by timaer 18 years ago
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]
Advertisement
You can make it an marco.
if (*pYou == ASSHOLE) { pYou->Die(); delete pYou; };
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?
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
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 ;(
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...
Quote:Original post by rip-off
EG:

*** Source Snippet Removed ***


That's horrid :P

Dave
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
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.
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 );

This topic is closed to new replies.

Advertisement