WannaBNExpert
asked on
Function overloading
I have the following questions regarding function overloading in C++
1. Why is that the C++ compiler cannot distinguish between two functions when it has the same name and parameter list but different return type? Why can't name mangling take even the return type in to account so that the mangled names of functions with same names and parameter list but different return types, can be differnet.
2. Why does the C++ compiler considers the following two functions as redeclaration
void f(int)
void f(const int) //considered as redeclaration of above
while the following functions are considered as overloaded
void f(int*)
voif f (const int*) //overloads the function declared above
Thanks in advance.
1. Why is that the C++ compiler cannot distinguish between two functions when it has the same name and parameter list but different return type? Why can't name mangling take even the return type in to account so that the mangled names of functions with same names and parameter list but different return types, can be differnet.
2. Why does the C++ compiler considers the following two functions as redeclaration
void f(int)
void f(const int) //considered as redeclaration of above
while the following functions are considered as overloaded
void f(int*)
voif f (const int*) //overloads the function declared above
Thanks in advance.
forgot to add foo(a); and foo(c); after the assignments, my bad.
Sorry, I was in a hurry, for 1) consider:
#include <iostream.h>
int foo(int a) {
cout << a << "\n";
return a;
}
double foo(int a) {
cout << a << "\n";
return (double) a;
}
int main() { foo(5); }
Now suppose that this works (it won't compile obviously) which foo is it d'you think?
#include <iostream.h>
int foo(int a) {
cout << a << "\n";
return a;
}
double foo(int a) {
cout << a << "\n";
return (double) a;
}
int main() { foo(5); }
Now suppose that this works (it won't compile obviously) which foo is it d'you think?
ASKER
But going by that argument how does the compiler distinguish between
void foo(int);
void foo(char);
It does some name mangling to ensure that the function names in the symbol tables are unique. For this it includes the parameter types along with the function name. Why can't it include the return type as well?
I did not quite get the answer to the second question. The code snippet does not contain any call to the functions defined.
void foo(int);
void foo(char);
It does some name mangling to ensure that the function names in the symbol tables are unique. For this it includes the parameter types along with the function name. Why can't it include the return type as well?
I did not quite get the answer to the second question. The code snippet does not contain any call to the functions defined.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For #2, I think I'd better explain some more. For foo(int) and foo(const int), if the user tries a literal integer, it could be either treated as a normal int or a const int, and this is troublesome. But int* and const int* are completely different types (one is a regular pointer and the other one is a constant pointer (once set it might not be reset to another memory location). Does that make any sense? If you want a pointer to a const (which I don't see any application for j/k) you'd write int* const, right?
WannaBNExpert:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
int foo(//same) {} and double foo(//same)
but when called like a void: foo(). There might be some other reasons that others could think of or are already in rules somewhere, but that's what I first have in mind.
2. A const pointer is different from a pointer to const.
foo(int) and foo(const int), which one is it for foo(5);
but for foo(int*) and foo(const int*) is different in the types that are being passed in.
Try this:
#include <iostream.h>
void foo(int* a) { cout << *a << "\n"; }
void foo(const int* a) {
cout << "Different\n" << *a << "\n";
}
int main() {
int* a, b = 5;
const int* c;
a = &b;
c = &b;
}
and see for yourself.