14.2.1: Functions that cannot be overloaded in C++
- Page ID
- 49969
Overload NOT Allowed
In C++ there are 6 types of function that CANNOT be overloaded. We will review and discuss them below.
Function declarations that differ only in the return type
In C++ (and Java), functions can not be overloaded if they differ only in the return type.
For example, the following program C++ programs will produce errors when compiled.
#include<iostream>
int foo() {
return 10;
}
char foo() { // compiler error; new declaration of foo()
return 'a';
}
int main()
{
char x = foo();
getchar();
return 0;
}
There are 2 foo() functions - and C++ cannot determine which one to use since they both have no arguments. The only difference between these two functions is the return type - therefore the compiler will flag this error.
Function declarations with the same name and the name parameter-type-list
#include<iostream>
class Test {
static void fun(int i) {}
void fun(int i) {}
};
int main()
{
Test myTest;
myTest.fun();
return 0;
}
If any of the member functions are declared as a static member function - then they cannot be overloaded. The compiler flags this as an error:
error: ‘void Test::fun(int)’ cannot be overloaded
void fun(int i) {}
^~~
error: with ‘static void Test::fun(int)’
static void fun(int i) {}
^~~
Parameter declarations that differ only in a pointer * versus an array []
The pointer and array are seen by the compiler as equivalent.
#include<iostream>
class Test {
int fun(int *ptr);
int fun(int ptr[]); // redeclaration of fun(int *ptr)
};
int main()
{
Test myTest;
myTest.fun();
return 0;
}
These declarations cause an error because the way that an array reference is passed is the same way that a pointer is passed.
error: ‘int Test::fun(int*)’ cannot be overloaded
int fun(int ptr[]); // redeclaration of fun(int *ptr)
^~~
error: with ‘int Test::fun(int*)’
int fun(int *ptr);
^~~
Parameter declarations that differ only in one is a function type and the other is a pointer to the same function type
#include<iostream>
class Test {
void fun(int ());
void fun(int (*)()); // redeclaration of fun(int())
};
int main()
{
Test myTest;
myTest.fun();
return 0;
}
C++ has difficulties determining between these two function calls...so the compiler flags it as an error.
error: ‘void Test::fun(int (*)())’ cannot be overloaded
void fun(int (*)()); // redeclaration of fun(int())
^
Parameter declarations that differ only in the presence or absence of const and/or volatile
Again - C++ sees these types of arguments as the same and therefore cannot distinguish between them.
#include<iostream>
class Test {
int fun ( int x) {
return x+10;
}
int fun ( const int x) {
return x+10;
}
};
int main()
{
Test myTest;
myTest.fun();
return 0;
}
Only the const and volatile type-specifiers at the outermost level of the parameter type specification are ignored in this fashion; const and volatile type-specifiers buried within a parameter type specification are significant and can be used to distinguish overloaded function declarations.
Two parameter declarations that differ only in their default arguments
The two functions are the same - it is just that one is coded with a default value. This still causes C++ to be unable to distinguis between the two functions.
#include<iostream>
#include<stdio.h>
using namespace std;
int fun ( int x, int y) {
return x+10;
}
int fun ( int x, int y = 10) {
return x+y;
}
int main() {
Test myTest;
myTest.fun();
return 0;
}
Adapted from:
"Function overloading and return type" by GeeksforGeeks, Geeks for Geeks is licensed under CC BY-SA 4.0
"Functions that cannot be overloaded in C++" by GeeksforGeeks, Geeks for Geeks is licensed under CC BY-SA 4.0