Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

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.
0
WannaBNExpert
Asked:
WannaBNExpert
  • 5
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now