boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - c++ question
相关主题
C++里面 class的数据成员的顺序是什么样的?
C++ Q16: dereferencing
还是成员函数指针,试试这个诡异的东东吧。
function pointer 和 call-back function 有什么区别?
one more interview question
Python Q: function pass in struct pointer, come back with data filled
问个c++ struct和指针问题
请问可以这样定义struct吗?
about STL functor and function pointers
C++ Function Pointer Array 的问题
相关话题的讨论汇总
话题: function话题: foo话题: int话题: pointer话题: member
进入Programming版参与讨论
1 (共1页)
c*******9
发帖数: 6411
1
Can anyone explain what the last statement means? thanks.
struct X{
int foo(){ return 0;}
}x;
struct Y{
static int(X::*p)();
};
int (x::*Y::p)() = X::foo;
X****r
发帖数: 3557
2
I don't think you have the right syntax here. It should be
int (X::*Y::p)() = &X::foo;
It is just to assign "&X::foo" (a pointer-to-member-of-X function
that returns int) to "Y::p" (a static member variable), which has
the type "int (X::*)()" (again, a pointer-to-member-of-X function
that returns int).

【在 c*******9 的大作中提到】
: Can anyone explain what the last statement means? thanks.
: struct X{
: int foo(){ return 0;}
: }x;
: struct Y{
: static int(X::*p)();
: };
: int (x::*Y::p)() = X::foo;

c*******9
发帖数: 6411
3
Thanks Xentar.
I look at the question again in brainbench, it is
int (X::*Y::p)() = X::foo; without "&"
maybe they are wrong? but isnot the function name itself can be used as
function pointer...
X****r
发帖数: 3557
4
I think they are wrong, because what you said is called
function-to-pointer conversion:
4.3 Function-to-pointer conversion [conv.func]
1 An lvalue of function type T can be converted to an rvalue of type
"pointer to T." The result is a pointer to the function.
However, there is a footnote specifically saying:
* This conversion never applies to nonstatic member functions because
an lvalue that refers to a nonstatic member function cannot be ob-
tained.

as

【在 c*******9 的大作中提到】
: Thanks Xentar.
: I look at the question again in brainbench, it is
: int (X::*Y::p)() = X::foo; without "&"
: maybe they are wrong? but isnot the function name itself can be used as
: function pointer...

c*******9
发帖数: 6411
5
Thanks a lot!
z****e
发帖数: 2024
6
非常赞红猪侠说的那个"&".我也受教了。
thrust以前也提醒过这个"&"。需要牢记啊!
我再狗尾续貂罗嗦几句,你可以这样理解:
成员函数指针的定义是:
返回值 (类名::*指针名)(参数表)=&类名:: 成员函数名;
成员函数名: foo
返回值: int
参数表: 空
类名:X
指针名: Y::p
由于 p 是static的,所以必须定义。而,
int (X::*Y::p)() = &X::foo;
就是p的定义。

【在 c*******9 的大作中提到】
: Can anyone explain what the last statement means? thanks.
: struct X{
: int foo(){ return 0;}
: }x;
: struct Y{
: static int(X::*p)();
: };
: int (x::*Y::p)() = X::foo;

B*******g
发帖数: 1593
7
唉 我觉得这已经是奇巧淫技的范畴了 :(

【在 z****e 的大作中提到】
: 非常赞红猪侠说的那个"&".我也受教了。
: thrust以前也提醒过这个"&"。需要牢记啊!
: 我再狗尾续貂罗嗦几句,你可以这样理解:
: 成员函数指针的定义是:
: 返回值 (类名::*指针名)(参数表)=&类名:: 成员函数名;
: 成员函数名: foo
: 返回值: int
: 参数表: 空
: 类名:X
: 指针名: Y::p

X****r
发帖数: 3557
8
这个只是语法规则而已,其实很容易的。
我感觉的奇巧淫技是类似C++ template meta programming里的
"Substitution failure is not an error"那样的。

【在 B*******g 的大作中提到】
: 唉 我觉得这已经是奇巧淫技的范畴了 :(
z****e
发帖数: 2024
9
meta programming,我看都没看。觉得那个根本就是另外的编程方法。

【在 X****r 的大作中提到】
: 这个只是语法规则而已,其实很容易的。
: 我感觉的奇巧淫技是类似C++ template meta programming里的
: "Substitution failure is not an error"那样的。

t****t
发帖数: 6806
10
我觉得这个多数情况下library都给你包装好了你用结果就行了.一般不需要用到吧?
有什么需要的例子吗?

【在 X****r 的大作中提到】
: 这个只是语法规则而已,其实很容易的。
: 我感觉的奇巧淫技是类似C++ template meta programming里的
: "Substitution failure is not an error"那样的。

相关主题
function pointer 和 call-back function 有什么区别?
one more interview question
Python Q: function pass in struct pointer, come back with data filled
问个c++ struct和指针问题
进入Programming版参与讨论
h*****0
发帖数: 4889
11
第一个写错了吧,掉了一个typedef

【在 c*******9 的大作中提到】
: Can anyone explain what the last statement means? thanks.
: struct X{
: int foo(){ return 0;}
: }x;
: struct Y{
: static int(X::*p)();
: };
: int (x::*Y::p)() = X::foo;

l******e
发帖数: 12192
12
sfinae挺实用的

【在 X****r 的大作中提到】
: 这个只是语法规则而已,其实很容易的。
: 我感觉的奇巧淫技是类似C++ template meta programming里的
: "Substitution failure is not an error"那样的。

z****e
发帖数: 2024
13
不是,
x 是个对象。
typedef 的话,才是类型。

【在 h*****0 的大作中提到】
: 第一个写错了吧,掉了一个typedef
z****e
发帖数: 2024
14
感觉只有写lib的人才会用meta programming吧。

【在 t****t 的大作中提到】
: 我觉得这个多数情况下library都给你包装好了你用结果就行了.一般不需要用到吧?
: 有什么需要的例子吗?

h*****0
发帖数: 4889
15
是对象的话,后面的显然写错了。

【在 z****e 的大作中提到】
: 不是,
: x 是个对象。
: typedef 的话,才是类型。

d****p
发帖数: 685
16
Not really. It is powerful tool for stuff like boost lib, but you can use in
routine work as well.
For example, you have a client which wants to call some functions against a
object (T). Normal OO approach is
that:
1. Define T as abstract function with virtual function foo()
2. Define several derived classes realizing T
3. Pass a T* to the client and call the function t->foo()
So the client function looks like
Bar(T* t) { t->foo(); }
Good but incur a virtual function call while increasing size o

【在 z****e 的大作中提到】
: 感觉只有写lib的人才会用meta programming吧。
z****e
发帖数: 2024
17
你这个例子很清晰啊,面试不会问这种吧,这我就挂定了。
那本modern design,我没有计划毕业前去看啊。

in
a

【在 d****p 的大作中提到】
: Not really. It is powerful tool for stuff like boost lib, but you can use in
: routine work as well.
: For example, you have a client which wants to call some functions against a
: object (T). Normal OO approach is
: that:
: 1. Define T as abstract function with virtual function foo()
: 2. Define several derived classes realizing T
: 3. Pass a T* to the client and call the function t->foo()
: So the client function looks like
: Bar(T* t) { t->foo(); }

1 (共1页)
进入Programming版参与讨论
相关主题
C++ Function Pointer Array 的问题
C++ Q90 - Q92 (转载)
菜鸟问个C++问题
这个function pointer最后的那个int是什么意思?
请教大家一道C的面试题
容器中放置智能指针一问
Why do I need to use "plain" pointer?
一道面试题
is smart_ptr really that good?
pointer overflow
相关话题的讨论汇总
话题: function话题: foo话题: int话题: pointer话题: member