# Function parameter accepting multiple types of objects is it possible?

This topic is 3468 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 weapons
{
int numberofbullets;
int rateoffire;
char szweaponname[50];
};

struct items
{
int Ammo;
int Health;
};

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;
// process struct....
break;
case WEAPONS:
// process struct.....
break;
};// end of switch

}// end of function



##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
Quote:
 Original post by rip-offFunction 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.

##### Share on other sites
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?

##### Share on other sites
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.

##### Share on other sites
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!).

• 38
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631365
• Total Posts
2999585
×