Function that can accept different paramter types possible?

Hello, I am back with more weird questions ...

Is it possible to create a function that has one paramter (for example), but that parameter can be either CustomClass1, CustomClass2, ... ?

And the function would of course have to determine what type the parameter is ...

Maybe something with pointers?
But no, that won't work becccause you have to specify what type of point it is ...

Any throughts on if/how this could be done?
(In a n00b-freindly way, so I can understand it ... )


Why?
I could think of several examples where this could be useful:
For example implementing an "interact with"-feature for a game.
The player can interact with different types of "things" (other "people" (NPCs), furniture, static objects, ... )
Each of these "type of thing" would be implemented with a class.
So, a function like "Player.InteractWith(//any type here)" would be needed.
Im a little confused at what exactly you want to achieve, do you want to pass class objects to a functions argument list? Or create a function that can take different arguments without knowing the type?

Maybe try looking at Function Overloading and Templates?

So, a function like "Player.InteractWith(//any type here)" would be needed.


You can do that with a class, for example you can make a player class and have an interact function then an interactable class and pass that classes object to the player classes InteractWith function and then you could pass in anything that you want to interact with.

1
2
3
4
5
Player.InteractWith(Interactable& interactable)
{
    interactable.OpenDoor();
    interactable.Read();
}


You can create classes for things like NPC's, items, Props etc that can inherit from the interactable class. Just try to avoid Multiple inheritance, or you're going to have quite a headache. But generally thats pretty easy to avoid if you plan your classes well.

https://www.geeksforgeeks.org/multiple-inheritance-in-c/


If you want one function name but want it to use different classes then you can simply overload it:

1
2
3
4
Player.InteractWith(Class2& class2)
{
    class2.DoSomething();
}



etc, something like that. Hopefully that clarifies something.
Last edited on
Thanks for the quick reply!

Yes, I want a function that can take multiple different types of paramters.

Let's take an example that might better explain it:

1
2
3
4
5
//Function that takes whatever, converts it to a string and outputs it
void PrintThis (InputVariable //string or int or char or float or ...)
  {
    cout << ConvertToString(InputVariable);
  }


This might be a better example ...
Now imagine something like that but instead of taking string,ints,chars, ... it would take different classes/objects as parameters.
Yeah I would say take a look at templates, function and class templates specifically. Templates allow for that kind of behavior. I would look at function overloading as well.

https://www.learncpp.com/cpp-tutorial/function-templates/
https://www.learncpp.com/cpp-tutorial/template-classes/
https://www.learncpp.com/cpp-tutorial/function-overloading/
Your example uses <<. That is an overloaded function, which makes this possible:
cout << 3.14f << " answer " << 42 << '\n';
(Four function calls, to the float, const char*, int, and char overloads.)

Custom types can provide their own overloads:
1
2
3
4
std::ostream& operator<< ( std::ostream&, const MyType& );

MyType sample;
cout << sample;


If you don't want to write each overload of PrintThis manually, then you can use templates:
1
2
3
4
template <typename T>
void PrintThis( const T& t ) {
  cout << t; // overloads of operator<< handle this
}


However, if a type can't have overload for <<, or you want different output than what the << would give, then you have to write separate PrintThis for that type.
Ok, thanks, I will look into that ...

Until now I managed to avoid those topics because they seemed "weird" and "not really usefull", but now I have a reason to learn about those features!

Thanks!


By the way, "overloading functions" sounds like a bad thing ...
Like, something to avoid ...
But I guess that is just the name they went with (loading something over something else, as in "on top of" something else, I guess ...)
;-)
Last edited on
Overloading functions simply means you can have a multiple functions with one name that take different arguments, like so:

void Add(int x, int y);
void Add(float x, float y);
void Add(double x, double y);

Now you can call add and the compiler will figure out which one of the functions to call based on what you input, int, float, double etc.
By the way, "overloading functions" sounds like a bad thing ...
Absolut not, modern C++ probably couldn't exist without it.

About passing different types to a function:
1
2
3
4
5
template<class T>
void foo(const T& val)
{
// use val
}


Another option is std::any, or if you can limit the types, std::variant.
I know that "overloading" isn't a bad thing ...
I just meant that as a joke ...

But thanks for the explainations!
I think that is exactly what I want / need!
Topic archived. No new replies allowed.