In C++, we are allowed to have two functions of the same name provided that the function parameter lists are different.
This is called, "overloading."
#include <iostream>
using namespace std;
float foo(float var){
// OVERLOADED FUNCTION
return 10.0;
}
int foo(int var) {
// OVERLOADED FUNCTION
return 0;
}
int main() {
cout << foo(-5) << endl;
cout foo(5.5f) << endl;
return 0;
}
I was wondering, are we allowed to have two template classes of the same name, so long as the lists of parameters to the template classes are different?
#include <iostream>
using namespace std;
template<typename SpamType>
class Klaus
{
public:
SpamType mySpam;
void print () {
cout << "SPAM" << endl;
return 0;
}
};
template<int Whites, int Yolks>
class Klaus
{
public:
int myEggs[Whites][Yolks];
void print () {
cout << "EGGS" << endl;
return 0;
}
};
int main()
{
Klaus<2, 5> eggKlaus;
Klaus<float> spamKlaus;
eggKlaus.print(); // prints `EGGS`
spamKlaus.print(); // prints `SPAM`
return 0;
}