Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Can I store a type as a variable in C?

Name: Anonymous 2008-06-08 7:24

printf("Hello World, /prog/\n");

I was wondering if there is a good way to store a C data type as a variable, and later use that variable for the declaration of return types and variable types (in the form of a cast would be fine), or as function argument for polymorphic fun.

Name: Paul Graham !LISPHymEeU 2008-06-08 11:04

>>5
do you think that all of your wonderful higher-level programming structures were shat fully-formed from the immaculate anus of Paul Graham
But! They are!

>>1,6,8
Use C++'s typeid instead, it's much nicer. Then you don't need a type argument since your function can just find it out itself.

I don't see why you really need to do that though, usually a polymorphic function should only use the common interface for all the objects it takes. It's part of the abstraction, if some objects need specific operations it should be hidden in their method and not handled by the polymorphic function. There are some valid uses for it, but just like a code full of casts, it may hint at a design problem.

later use that variable for the declaration of return types and variable types
You can't declare at runtime. You mean force the output with a cast to be the type you declared? This is nasty, if you want to learn anything useful try to make your code as simple and maintainable as possible (eg: avoid this). Like, return a pointer to a base class or something. Besides, your function can't take in random objects, unless you make it take a void* pointer - which is also nasty shit, and you won't be able to dereference it anyway.

If you want a function that takes multiple types and there's any way at all of making it known at compile-time, try to template it instead.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List