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:
Anonymous2008-06-08 7:28
typedef
Name:
Anonymous2008-06-08 7:33
>>2
No, I want to be able to hold in a variable "This is an int" or "This is a float" (for any arbitrary type), not rename int to hurrdurr_t.
Unless there is some clever trick using typedef of which I am unaware.
Name:
Anonymous2008-06-08 7:59
//`'''```,
o // LISP `.,
,....OOo. .c;.',,,.'``.,,.`
.' ____.,'.//
/ _____ \___/.'
| / || \\---\|
|| || \\ ||
co co co co
Name:
Anonymous2008-06-08 8:05
>>4
No fuck you; I'm doing this because I want to learn how to implement crazy shit at a lower level. Or do you think that all of your wonderful higher-level programming structures were shat fully-formed from the immaculate anus of Paul Graham?
>>7
__typeof__ is an extension supported by some compilers.
It's not really standard C. What you're asking for cannot be done that way. You have to find another solution if you want your code to be portable.
Name:
Paul Graham!LISPHymEeU2008-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.
You can implement polymorphism of this kind in C, however:
1. YOU will have to implement handling of all possible types and the logic that chooses them.
2. Compiler will not help you -- you will have to pass the type as a value of some variable declared as enum.
3. If you were able to implement this properly, you wouldn't ask for it here.
Name:
Anonymous2008-06-09 5:27
use a union!
Name:
Anonymous2008-06-09 20:07
//`'''```,
o // JAVA `.,
,....OOo. .c;.',,,.'``.,,.`
.' ____.,'.//
/ _____ \___/.'
| / || \\---\|
|| || \\ ||
co co co co
Name:
Anonymous2008-06-09 20:12
its not hard at all. create a struct with a union of all of the types in c inside of it, and an enum to tell you which type it is
like so:
enum c_type {
INT,
SHORT,
USHORT,
ULONG,
...
};
struct type {
enum c_type type;
union {
int a;
short b;
unsigned short c;
unsigned long d;
...
} storage;
};
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy