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.
WannaBNExpertAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

n_fortynineCommented:
1. Consider someone who calls a function in a C-like way.
 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.
0
n_fortynineCommented:
forgot to add foo(a); and foo(c); after the assignments, my bad.
0
n_fortynineCommented:
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?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

WannaBNExpertAuthor Commented:
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.
0
n_fortynineCommented:
I said in the 2nd post that I forgot to add foo(a); and foo(c) after the assignments. So the correct one should be

#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;
  foo(a);
  foo(c);
}

Regarding 1) I think I picked a bad example. Say you have

int foo(int a) {
  cout << a << "\n";
  return a;
}

double foo(int a) {
  cout << a << "\n";
  return (double) a;
}

int main() {
  int a = 5;
  foo(a); //C++ let you call a function w/o return type.
          //This is bad because the compiler wouldn't know
          //which foo() to use. I bet there are better
          //reasons than this, but I said this is just an
          //idea that came to my mind. Why don't you dig
          //around and look for some others?
}

foo(5) was just a bad example. Of course the compiler will look at the type being passed in, that's how foo(int) and foo(char) are distinguised just as you said.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
n_fortynineCommented:
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?
0
CleanupPingCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.