• Advertisement
Sign in to follow this  

how to pass class type with out templates

This topic is 4580 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 used templates before, but is there a way to pass "class" type through a regular function's parameters with out using templates. I wont use conditions or strings or class IDs. Just need to know if the compiler has a mechanism (C++) for passing class types. (No templates either) Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by pcxmac
I have used templates before, but is there a way to pass "class" type through a regular function's parameters with out using templates. I wont use conditions or strings or class IDs. Just need to know if the compiler has a mechanism (C++) for passing class types. (No templates either) Thanks.


Pass the RTTI type info...

void function(const type_info &info)
{
//.....something here
}


function(typeid(classname));

Share this post


Link to post
Share on other sites
You either use templates, or you gotta pass it some variable to describe the type. Those are your options. Use templates if at all possible. Perhaps you should explain why you're against using templates?

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
Perhaps you should explain why you're against using templates?

Yeah, this is what I'm wondering. What you describe is one of the main uses of templates.

Share this post


Link to post
Share on other sites
to tell the truth I think it makes my code look really crappy. I should deal with it, but if I could instead of using templates have a variable which directly held the class type, not some descriptor, It would go along way to cleaning up my code, and making it flow better for me. And I do not believe in registries. Im a fan of the "prefrence file" and plug in archatecture like system 7.5 (macos) oooh those were the days. Thanks a bunch for the info though, I will look in to RTTI. Input is much appreciated.

Share this post


Link to post
Share on other sites
Quote:
Original post by pcxmac
to tell the truth I think it makes my code look really crappy.


[rolleyes]

Quote:
I should deal with it, but if I could instead of using templates have a variable which directly held the class type, not some descriptor


That's not possible in C++. Period. While a typeinfo object lets you know the type of a variable, it does not act as a type token: you can't use it to create a variable of that type, for example.

Quote:
Input is much appreciated.


Explain what you want to achieve.

Share this post


Link to post
Share on other sites
I agree that template syntax looks bad, but it works and it's better than the alternative:



const int idPERSON = 0;
const int idMECHANIC = 1;
const int idARTIST = 2;
const int idATHLETE = 3;

etc...


function dosomething(const int typeid, void *object)
{
switch(typeid)
{
switch(idPERSON):
...
break;
switch(idMECHANIC):
break;
... etc
}
}



Share this post


Link to post
Share on other sites
Quote:
Original post by pcxmac
I have used templates before, but is there a way to pass "class" type through a regular function's parameters with out using templates. I wont use conditions or strings or class IDs. Just need to know if the compiler has a mechanism (C++) for passing class types. (No templates either) Thanks.
While not as flexible as templates you could use polymorphic classes. If your function takes the base class as a parameter, you can pass any derived class so long as the base class has virtual functions.

Share this post


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

  • Advertisement