?
Solved

Function overloading

Posted on 2003-03-02
8
Medium Priority
?
242 Views
Last Modified: 2010-04-17
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.
0
Comment
Question by:WannaBNExpert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
8 Comments
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8055297
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
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8055300
forgot to add foo(a); and foo(c); after the assignments, my bad.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8055308
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:WannaBNExpert
ID: 8056023
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
 
LVL 4

Accepted Solution

by:
n_fortynine earned 220 total points
ID: 8056100
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
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8056121
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
 

Expert Comment

by:CleanupPing
ID: 9447402
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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn different types of Android Layout and some basics of an Android App.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Six Sigma Control Plans

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question