由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ func overload question
相关主题
C++ function template问题Java banned operator overloading
one question about overloading operator delete请问一个implicit conversion的问题(C++)
python 可以给class动态添加method吗?这个问题怎么答?
【讨论】问一道很简单的C++题。。。。 (转载)这段code有啥问题?
C++高手请进,内存管理问题How to overload global new operator?
[C++] 入门级问题 increment and decrement operatorsoperator overloading (C++)
C++ class template specialization questionC++ Q89: function template and overloading (转载)
why use static function here?请问关于overloading <<
相关话题的讨论汇总
话题: int话题: void话题: double话题: funptr话题: cout
进入Programming版参与讨论
1 (共1页)
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
6
你们这样会把人教坏了的。
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);

相关主题
[C++] 入门级问题 increment and decrement operatorsJava banned operator overloading
C++ class template specialization question请问一个implicit conversion的问题(C++)
why use static function here?这个问题怎么答?
进入Programming版参与讨论
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?

1 (共1页)
进入Programming版参与讨论
相关主题
请问关于overloading <<C++高手请进,内存管理问题
问题的根源找到了[C++] 入门级问题 increment and decrement operators
Go不支持operator overloadC++ class template specialization question
ambiguous operators in c++why use static function here?
C++ function template问题Java banned operator overloading
one question about overloading operator delete请问一个implicit conversion的问题(C++)
python 可以给class动态添加method吗?这个问题怎么答?
【讨论】问一道很简单的C++题。。。。 (转载)这段code有啥问题?
相关话题的讨论汇总
话题: int话题: void话题: double话题: funptr话题: cout