Overloading a function - removing ambiguity with a cast : overloading ambiguity « Function « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

double larger(double a, double b);             
long larger(long a, long b);                

int main() {
  int a_int = 15, b_int = 25;
  cout << larger(static_cast<long>(a_int), static_cast<long>(b_int))
       << endl;

return 0;
}

double larger(double a, double b) {
  cout << "double larger() called" << endl;
  return a>b ? a : b;
}

long larger(long a, long b) {
  cout << "long larger() called" << endl;
  return a>b ? a : b;
}
long larger() called
25








7.14.overloading ambiguity
7.14.1.Overloading ambiguity: float and double
7.14.2.Overloading ambiguity: char and unsigned char
7.14.3.Overloading abiguity: default parameter
7.14.4.Overloading a function - removing ambiguity with a cast