Function Overloading
-
Dear Developers, Todays I found a surprsing fact on the scenario of functiona overlaoding in C++. The code segment is here #include using namespace std; void display( char* ); void display( const char* ); void main() { char *ch1 = "Hello"; const char *ch2 = "Bye"; display(ch1); display(ch2); } void display( char* p ) { cout << p; } void display( const char* p ) { cout << p; } As I know, In function overloading, the arguments must be differ in either in type or count. But its working. and one more surpring fact is that if I am remving pointers on that program. It says.. that function already have a body. How is it possible???? Please guide me. Thnaks. Amrit Agrawal Software Developer, Mumbai
-
Dear Developers, Todays I found a surprsing fact on the scenario of functiona overlaoding in C++. The code segment is here #include using namespace std; void display( char* ); void display( const char* ); void main() { char *ch1 = "Hello"; const char *ch2 = "Bye"; display(ch1); display(ch2); } void display( char* p ) { cout << p; } void display( const char* p ) { cout << p; } As I know, In function overloading, the arguments must be differ in either in type or count. But its working. and one more surpring fact is that if I am remving pointers on that program. It says.. that function already have a body. How is it possible???? Please guide me. Thnaks. Amrit Agrawal Software Developer, Mumbai
Use code tags when you post code. What do you mean by "removing pointers"?
Amrit Agr wrote:
the arguments must be differ in either in type or count. But its working.
The arguments to the methods have different types. And do you have warnings turned on to the highest level in your C++ compiler?
-
Dear Developers, Todays I found a surprsing fact on the scenario of functiona overlaoding in C++. The code segment is here #include using namespace std; void display( char* ); void display( const char* ); void main() { char *ch1 = "Hello"; const char *ch2 = "Bye"; display(ch1); display(ch2); } void display( char* p ) { cout << p; } void display( const char* p ) { cout << p; } As I know, In function overloading, the arguments must be differ in either in type or count. But its working. and one more surpring fact is that if I am remving pointers on that program. It says.. that function already have a body. How is it possible???? Please guide me. Thnaks. Amrit Agrawal Software Developer, Mumbai
Hi, Amrit, It's working because it's legal to overload functions based on whether the argument refers to a const or non-const type. So,
void display(const char*) //pointer pointing to a const type
is overloading
void display(char*) // plain pointer
And you're correct. The following couldn't be overloaded. It won't compile because of const conversion used by compiler. It can't figure out which one is the best match.
void display(char);
void display(const char);
void display(char const);(Technically, the above are redeclaration. It's fine to just define the third one by adding function body. But it won't compile if all of these three functions have function bodies or definitions.) By the way, to be clearer, the following won't compiler neither because of the same reason.
void display(char*){...} //plain pointer pointing to a regular type
void display(char* const){...} //const pointer pointing to a regular type