Function overloading by swapping order of parameters?

The book I'm using to learn C++ is talking about function overloading.

It states that 2 functions with the same name must have either a different amount of parameters, or some parameters of differing types.

So, for example, the following is legal, as both prototypes have different numbers of parameters:

1
2
void myFunc(int myVar, double myVar2);
void myFunc(int myVar, double myVar2, double myVar3);


as is (because there is at least 1 parameter of a different type):

1
2
void myFunc(int myVar, double myVar2);
void myFunc(double myVar, double myVar2);


What the book doesn't say, and I wondered, just for curiousity, is can you overload with the same number of parameters, of the same types, but in a different order. For example is the following legal:

1
2
void myFunc(int myVar, double myVar2);
void myFunc(double myVar2, int myVar);


Thanks. Note that in no way am I suggesting any of the above is a good idea/good practice, I just wondered, to satisfy my desire to learn as much as possible.
Yes gers1978, as long as there is some difference in the function parameters that the compiler can use to determine which function you want to call.
You can test your question with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void myFunc(int myVar, double myVar2)
{
	cout << "int then double\n";
}
void myFunc(double myVar2, int myVar)
{
	cout << "double then int\n";
}

int main()
{
int var1=0;
double var2=0;

	myFunc(var1,var2);
	myFunc(var2,var1);
	system("pause");
  return 0;
}
Last edited on
Topic archived. No new replies allowed.