Jump to content
  • Advertisement
Sign in to follow this  
nivlekio

Function parameter accepting multiple types of objects is it possible?

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

If you intended to correct an error in the post then please contact us.

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 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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!).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!