|
-
January 7th, 2012, 03:17 PM
#1
about overloading function
Code:
#include<iostream>
using namespace std;
void f1() {cout << "overload def#1 of f1()" << endl;} // def#1
void f1(int x) {cout << "overload def#2 of f1()" << endl;} // def#2
void f1(double x) {cout << "overload def#3 of f1()" << endl;} // def#3
void f1(double x, char y) {cout << "overload def#4 of f1()" << endl;} // def#4
void f1(int x, char y) {cout << "overload def#5 of f1()" << endl;} // def#5
void f1(double &x) {cout << "overload def#6 of f1()" << endl;} // def#6
int main()
{
/*1.*/ f1(2);
/*2.*/ f1();
/*3.*/ f1(2.5, 'A');
/*4.*/ f1(2, 'A');
double a = 2.5;
/*5.*/ f1(2.5);
/*6.*/ f1(a);
return 0;
} // end main()
I can't understand why function call 6 can't call def#3.isn't their type the same?
and if comment the function call at 6 i give default value to parameter x of def#2 ,it will be error function call 2.
can someone explain to me how is it ambiguous?. or give me the chapter name so i can go re-study.
at least give me a hint before i got more misunderstanding.
sorry for my english.
-
January 7th, 2012, 03:33 PM
#2
Re: about overloading function
It can't tell if you want to call 3 or 6.
-
January 7th, 2012, 03:52 PM
#3
Re: about overloading function
isn't it reference passing?
Code:
#include<iostream>
using namespace std;
void f1() {cout << "overload def#1 of f1()" << endl;} // def#1
void f1(int x=3) {cout << "overload def#2 of f1()" << endl;} // def#2
void f1(double x) {cout << "overload def#3 of f1()" << endl;} // def#3
void f1(double x, char y) {cout << "overload def#4 of f1()" << endl;} // def#4
void f1(int x, char y) {cout << "overload def#5 of f1()" << endl;} // def#5
void f1(double &x) {cout << "overload def#6 of f1()" << endl;} // def#6
int main()
{
/*1.*/ f1(2);
/*2.*/ f1(); /*<<<<this got error*/
/*3.*/ f1(2.5, 'A');
/*4.*/ f1(2, 'A');
double a = 2.5;
/*5.*/ f1(2.5);
return 0;
} // end main()
this got error.it say ambiguous. why f1() call def#1.
thanks for answer
-
January 7th, 2012, 04:33 PM
#4
Re: about overloading function
I can't understand why function call 6 can't call def#3.isn't their type the same?
When overloaded function is met, compiler deduces the closest matching variant from the function call. Calling like f1(a) matches to both f1(double) and f1(double&), so there's an ambiguity which cannot be solved by compiler.
To solve this, you need to signify which exact f1 version you need.
Code:
double a = 2.5;
/*5.*/ f1(2.5);
/*6.*/ ((void (*)(double &))f1)(a);
Or better you never introduce such ambiguous overloaded function prototypes.
Best regards,
Igor
-
January 7th, 2012, 06:35 PM
#5
Re: about overloading function
this got error.it say ambiguous. why f1() call def#1.
Code:
void f1();
void f1(int x=3);
//...
f1(); // which one will be called?
Both of the f1() functions can be called with no arguments. That's why it is ambiguous.
Regards,
Paul McKenzie
-
January 7th, 2012, 11:18 PM
#6
Re: about overloading function
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|