2

The code below is (apparently) correct syntax (using g++ in DevCpp), but there some things I seem to not understand. I have a good understanding of plain variables vs pointers vs references, but the finer points with how they work for function overloading elude me.

Is the reference-modifier alone really sufficient to differentiate between the 2 functions, and if both "funcA" functions below are defined, is there a way to call funcA(int&) ?

Or is this something that is outside the scope of defined behavior in the c++ standard?

(For the scope of this question I'm putting aside the const modifier, but there is a similar question here otherwise: Function Overloading Based on Value vs. Const Reference )

#include <iostream>
#include <iomanip>
using namespace std;

void funcA(int p1);
void funcA(int& p1);

int main()
{
  char e;
  int x = 0;

  //funcA(x);  //ambiguous
  //funcA((int&)x);  //ambiguous

  funcA((int)x);  //ok, calls   funcA(int p1)
  funcA(5);  //ok, calls   funcA(int p1)

  scanf("%c", &e);
  return 0;
}

void funcA(int p1)
{
  cout << " called int funcA(int p1) " << endl;
  return;
}

void funcA(int& p1)
{
  cout << " called int funcA(int& p1) " << endl;
  return;
}
Community
  • 1
  • 1
user320781
  • 303
  • 4
  • 7
  • @GWW Thanks, didn't see that with the different tags. Ultimately, yes dupe. Answer from Dietmar Kuhl in that question would be: funcA(const_cast(x)); //ok, calls funcA(int p1) static_cast(funcA)(x); //ok, calls funcA(int p1) – user320781 May 23 '14 at 16:37

0 Answers0