how to pass class type with out templates

Started by
7 comments, last by Grain 18 years, 8 months ago
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.
sig
Advertisement
Not really.
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));
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?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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.
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.
sig
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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    }}
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.

This topic is closed to new replies.

Advertisement