Function parameter accepting multiple types of objects is it possible?

Started by
5 comments, last by bluntman 15 years, 4 months ago
I was wondering if it is possible to make a function parameter accept any type of object. I know of function overloading but i wanted to know if there is another way? Say for example different structs into the name function parameter. Then when you pass in a struct in to the function, there will be code that determines the struct type, then you do what ever you want to do with the struct. Here is an example.

struct position
{
int xpos;
int ypos;
};

struct badguys
{
char szBadguyName[50];
int BadGuyHealth;
int BadGuyID;
};

struct weapons
{
int numberofbullets;
int rateoffire;
char szweaponname[50];
};

struct items
{
int Ammo;
int Health;
int granades;
};

struct playerinfo
{
int numofkills;
int numofdeaths;
int numofweapons
};

void ProcessStructs(parameter (pass in a struct of any type))
{
int StructType;
/////////////
//     code for finding out what type the struct is happens here
////////////

// when u find out the struct type then process it here.
switch(StructType)
{
case POSITION:
// process struct....
break;
case BADGUYS:
// process struct....
break;
case WEAPONS:
// process struct.....
break;
};// end of switch

}// end of function

Thanks for your help in advance
Advertisement
You can use runtime polymorphism based on either object-oriented subtypes or on functional programming. All of this requires the function parameter to have a single type, which will be a supertype of all object types it can accept.
Function overloading is actually a nice choice here, because you cleanly separate the logic for each of the different struct types.

Can you really think of a function that should be able to do meaningful work on all these unrelated structures?
Quote:Original post by rip-off
Function overloading is actually a nice choice here, because you cleanly separate the logic for each of the different struct types.

Can you really think of a function that should be able to do meaningful work on all these unrelated structures?


Agreed. Function overloading does exactly what is you're trying to accomplish, only much cleaner.

But - if you want to go the hard route, polymorphism would be the way to go. But honestly, I'd rather just stick with function overloading. Much more elegant.

From a clarity viewpoint, you probably don't want a generically named function which just "processes" structs, if the interal logic of the functions is totally different for each struct. How about just giving the functions descriptive names and calling them on the related structures? From your code it doesn't look like the structures inherit from a single base class so you couldn't be calling these functions in a loop on an array of these structures, so why do you want a single function to process them all?
I was just giving an example, i just thought it from my head its not actual code i use. I know overloading functions would work better for that situtation, could not think of a better example situtation for having a function paramrter that can take in multiple object types.

But any ways thanks for the help ill look up this polymorphism buisness.
Be careful with polymorphism as it is ripe for abuse and misuse in design as well as technical usage (having been there and done that myself). e.g. Putting types into a heirarchy when they do not have an "IS-A"/"IS-LIKE-A" relationship (design misuse), or calling virtual functions from constructors (technical misuse, can't think of a better term at the moment!).

This topic is closed to new replies.

Advertisement