GingerRobot Posted March 17, 2009 Share Posted March 17, 2009 At the moment i'm working on something which requires linked lists of various types. It occurs to me that i can make a generic list type with structures and unions. So i might have a structure for an integer list, one for a char list, one for a list of my own type etc and i'll then have a union of them inside another structure along with some flag (probably an enum) to tell me what the list type is. That's all fine and dandy, but i'm not sure what the best way to work with it is. For example, i'll need functions to add to the end of the list but i'd really rather not have to write separate functions for inserting with each type (e.g. i don't want to have add_to_int_list(), add_to_char_list() etc). So to the question: is there anyway to avoid this? Can i somehow pass whatever type i like into a function? On the one hand it seems unlikely, but then what about functions such as printf() which take a variable number of arguments of variable type? Thanks in advance for any input Quote Link to comment https://forums.phpfreaks.com/topic/149800-solved-c-using-unions-to-implement-something-like-a-mixedgeneric-type/ Share on other sites More sharing options...
corbin Posted March 17, 2009 Share Posted March 17, 2009 In C++ you might could manage it with templates.... Or you could just wrap it in a class and have the .push method do everything. As far as C goes, you will just have to do a lot of different functions as far as I can tell. You can of course have a function with the same name as another function and different arguments. As for how printf does what it does, it does it using va_arg. (I've used it before to emulate PHP's implementation of pack.) Is I understand your question, va_arg will not help you any. Quote Link to comment https://forums.phpfreaks.com/topic/149800-solved-c-using-unions-to-implement-something-like-a-mixedgeneric-type/#findComment-787131 Share on other sites More sharing options...
GingerRobot Posted March 20, 2009 Author Share Posted March 20, 2009 Yeah, it certainly looks that way. I guess it would be possible to have another union of all the possible types that you wanted to pass in, but that'd just be messy and have a lot of overhead. Ah well. Thanks anyway Quote Link to comment https://forums.phpfreaks.com/topic/149800-solved-c-using-unions-to-implement-something-like-a-mixedgeneric-type/#findComment-789410 Share on other sites More sharing options...
corbin Posted March 21, 2009 Share Posted March 21, 2009 ;p Quote Link to comment https://forums.phpfreaks.com/topic/149800-solved-c-using-unions-to-implement-something-like-a-mixedgeneric-type/#findComment-790107 Share on other sites More sharing options...
DarkWater Posted March 21, 2009 Share Posted March 21, 2009 Have the functions accept data of type void *, which is a pointer to a void. That's actually C's way of implementing a generic pointer; it can point to any kind of data. Now, you can either use that data directly as is, or you can make a copy of it so the initial code can free the pointer. Quote Link to comment https://forums.phpfreaks.com/topic/149800-solved-c-using-unions-to-implement-something-like-a-mixedgeneric-type/#findComment-790526 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.