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.
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.
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.
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.)
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.
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 ...)
;-)