j****i 发帖数: 305 | 1 #include
using namespace std;
class A
{
public:
void f(int) {cout << "FINT\n";}
void f(int, double j = 0) {cout << "FINTDOUBLE\n";}
};
main()
{
A a;
// a.f(3);
}
Without the commented line, the program compiles; with it, it reports error:
a.cpp: In function 'int main()':
a.cpp:14: error: call of overloaded 'f(int)' is ambiguous
a.cpp:7: note: candidates are: void A::f(int)
a.cpp:8: note: void A::f(int, double)
How to resolve this? And how |
d****p 发帖数: 685 | 2 You are really asking for trouble :-)
int (A::*funPtr)(int);
funPtr = &A::f;
A a;
(a.*funPtr)(n);
【在 j****i 的大作中提到】 : #include : using namespace std; : class A : { : public: : void f(int) {cout << "FINT\n";} : void f(int, double j = 0) {cout << "FINTDOUBLE\n";} : }; : main() : {
|
p********o 发帖数: 640 | 3 awesome!
【在 d****p 的大作中提到】 : You are really asking for trouble :-) : int (A::*funPtr)(int); : funPtr = &A::f; : A a; : (a.*funPtr)(n);
|
j****i 发帖数: 305 | 4 I had a sense that one could use func ptr, but there's no way that I could
come up with your solution!
Thanks.
【在 d****p 的大作中提到】 : You are really asking for trouble :-) : int (A::*funPtr)(int); : funPtr = &A::f; : A a; : (a.*funPtr)(n);
|
z****e 发帖数: 2024 | 5 a.f(3,0),也行
【在 d****p 的大作中提到】 : You are really asking for trouble :-) : int (A::*funPtr)(int); : funPtr = &A::f; : A a; : (a.*funPtr)(n);
|
f*****Q 发帖数: 1912 | |
r****t 发帖数: 10904 | 7 这个改 template 以后可以 partial 的 |
X****r 发帖数: 3557 | 8 One-liner would also work:
(a.*(void (A::*)(int))&A::f)(3);
【在 d****p 的大作中提到】 : You are really asking for trouble :-) : int (A::*funPtr)(int); : funPtr = &A::f; : A a; : (a.*funPtr)(n);
|
S**I 发帖数: 15689 | 9 这比岳博士的还要复杂 :)
【在 X****r 的大作中提到】 : One-liner would also work: : (a.*(void (A::*)(int))&A::f)(3);
|
d****p 发帖数: 685 | 10 If you guys want to be fired or turned down in interview, follow Xentar :-)
He is always one step more "uncivilized" than you could ever imagine.
Enough joke. I guess Xentar has a perl background.
【在 X****r 的大作中提到】 : One-liner would also work: : (a.*(void (A::*)(int))&A::f)(3);
|
|
|
d****p 发帖数: 685 | 11 This is not the function LP wants to call.
【在 z****e 的大作中提到】 : a.f(3,0),也行
|
X****r 发帖数: 3557 | 12 Yes, this is definitely not the recommended way to doing things.
But one shall never make two overloaded functions only
distinguishable by a default parameter to start with anyway :)
Actually I got this bad one-liner habit in Apple II BASIC days.
I did not realize that programs need structure until I started
to learn C and Pascal a few years later, but it was too late ;-)
【在 d****p 的大作中提到】 : If you guys want to be fired or turned down in interview, follow Xentar :-) : He is always one step more "uncivilized" than you could ever imagine. : Enough joke. I guess Xentar has a perl background.
|
z****e 发帖数: 2024 | 13 看到了。
【在 d****p 的大作中提到】 : This is not the function LP wants to call.
|
z****e 发帖数: 2024 | 14 强强制转化,呵呵。
【在 X****r 的大作中提到】 : One-liner would also work: : (a.*(void (A::*)(int))&A::f)(3);
|
l*****0 发帖数: 238 | 15 人们发明先进工具的目的是为了让事情变得更加简单,而不是相反。把程序写成(a.*(
void (A::*)(int))&A::f)(3);这个样子除了能给人带来挑战大脑极限的乐趣之外,对
要解决的问题似乎没什么真正的帮助,而且多半是背道而驰的。工具的目的和意义不是
工具本身,用最简单的方法写出最易懂的代码才是更加重要的事情 |
s*********s 发帖数: 109 | 16
your overloading is wrong since you have defined the first function twice.
void f (int, double j=0) is the same as the two following functions:
void f (int)
void f (int, double)
still remember default parameter constructors/functions?
【在 j****i 的大作中提到】 : #include : using namespace std; : class A : { : public: : void f(int) {cout << "FINT\n";} : void f(int, double j = 0) {cout << "FINTDOUBLE\n";} : }; : main() : {
|
N***m 发帖数: 4460 | 17 I don't think that's double definition.
It is just ambiguity in some cases.
【在 s*********s 的大作中提到】 : : your overloading is wrong since you have defined the first function twice. : void f (int, double j=0) is the same as the two following functions: : void f (int) : void f (int, double) : still remember default parameter constructors/functions?
|