由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ pointer to function is buggy
相关主题
int F::*x = &F::x是什么意思?问一个关于C++指针的问题
简单的c code问题刚看完类这一章,有些大小问题,请指教,谢谢
C puzzle 一日一题关于placement new
问一个volatile和memcpy一起用的问题问题
今天给c++震惊了[c++] reference 真得不能bound to a second object 么?
*(&b1)=b编译不过,b1=b可以,区别是?c++ interview question
大家要学习C++11啊, 我觉得C++11很多FEATURE,绝对不输JAVA.C++的"初始化"小结
[求教大虾]关于C++编译期变量和运行期变量的区别,总是有疑惑c++如果调用没参数的函数不用加()就好了
相关话题的讨论汇总
话题: ptof话题: subf话题: pointer话题: array话题: int
进入Programming版参与讨论
1 (共1页)
i**p
发帖数: 902
1
void subf() {}
void (*PtoF)(void);
1. PtoF = subf;
2. PtoF = &subf;
3. PtoF();
4. (*PtoF)();
1==2 hard to understand why it allows both
3==4 somehow easy to be understood
Is it good to allow both?
d****i
发帖数: 4809
2
你的函数指针的定义不对,应该对应函数的argument, 比如
void subf(int a, void *ptr) {}
void (*PtoF)(int, void*);
至于下面两个为什么一样,函数指针用函数名赋值和用函数的地址是等效的,可以想象
成函数名就是函数的入口地址。
PtoF = subf;
PtoF = &subf;

【在 i**p 的大作中提到】
: void subf() {}
: void (*PtoF)(void);
: 1. PtoF = subf;
: 2. PtoF = &subf;
: 3. PtoF();
: 4. (*PtoF)();
: 1==2 hard to understand why it allows both
: 3==4 somehow easy to be understood
: Is it good to allow both?

i**p
发帖数: 902
3
changed the function's arguments. Simplified the code before published, but
not all cleaned.

【在 d****i 的大作中提到】
: 你的函数指针的定义不对,应该对应函数的argument, 比如
: void subf(int a, void *ptr) {}
: void (*PtoF)(int, void*);
: 至于下面两个为什么一样,函数指针用函数名赋值和用函数的地址是等效的,可以想象
: 成函数名就是函数的入口地址。
: PtoF = subf;
: PtoF = &subf;

i**p
发帖数: 902
4
"函数指针用函数名赋值和用函数的地址是等效的,可以想象成函数名就是函数的入口
地址。"
So it is not consistent to usage &i which is i's address.
Any reason not to disable &subf to keep & consistency?

【在 d****i 的大作中提到】
: 你的函数指针的定义不对,应该对应函数的argument, 比如
: void subf(int a, void *ptr) {}
: void (*PtoF)(int, void*);
: 至于下面两个为什么一样,函数指针用函数名赋值和用函数的地址是等效的,可以想象
: 成函数名就是函数的入口地址。
: PtoF = subf;
: PtoF = &subf;

d****i
发帖数: 4809
5
I don't know the exact reason why it allows both. But you can also use array
name as the address of the first element of array as an analogy, right?
After all, code segment lives in different address space than data segment.
Functions are different from variables.

【在 i**p 的大作中提到】
: "函数指针用函数名赋值和用函数的地址是等效的,可以想象成函数名就是函数的入口
: 地址。"
: So it is not consistent to usage &i which is i's address.
: Any reason not to disable &subf to keep & consistency?

i**p
发帖数: 902
6
int arr[] = {1, 2, 3};
int *p;
p = arr; // OK
p = &arr; // error: cannot convert 'int (*)[3]' to 'int*' in assignment
p = (int *)&arr; // OK
So arr and &arr are different somehow.

array
.

【在 d****i 的大作中提到】
: I don't know the exact reason why it allows both. But you can also use array
: name as the address of the first element of array as an analogy, right?
: After all, code segment lives in different address space than data segment.
: Functions are different from variables.

l*********s
发帖数: 5409
7
that is the semantics of the language, I guess you have to live with it or
write your own.
i**p
发帖数: 902
8
Though we have to live with it, we still can make comments on it, right?
Do you really think you are super to say so?

【在 l*********s 的大作中提到】
: that is the semantics of the language, I guess you have to live with it or
: write your own.

b*******s
发帖数: 5216
9
是不一样的,有个隐式转换
你可以试试下面代码
#include
#include
using namespace std;
void f(int)
{}
void (*pf)(int);
int main()
{
pf = f;
pf = &f;
cout << "f:" << typeid(f).name() << endl;
cout << "&f:" << typeid(&f).name() << endl;
}

【在 i**p 的大作中提到】
: Though we have to live with it, we still can make comments on it, right?
: Do you really think you are super to say so?

l*********s
发帖数: 5409
10
sure, but why making a fuss over such a trifle. unless you are gathering
opinion to design your own language, if that is case, hat off a salute!

【在 i**p 的大作中提到】
: Though we have to live with it, we still can make comments on it, right?
: Do you really think you are super to say so?

相关主题
*(&b1)=b编译不过,b1=b可以,区别是?问一个关于C++指针的问题
大家要学习C++11啊, 我觉得C++11很多FEATURE,绝对不输JAVA.刚看完类这一章,有些大小问题,请指教,谢谢
[求教大虾]关于C++编译期变量和运行期变量的区别,总是有疑惑关于placement new
进入Programming版参与讨论
b*******s
发帖数: 5216
11
4.3 Function-to-pointer conversion [conv.func]
1 An lvalue of function type T can be converted to a prvalue of type “
pointer to T.” The result is a pointer tothe function.57
I think it is what happend.

【在 i**p 的大作中提到】
: Though we have to live with it, we still can make comments on it, right?
: Do you really think you are super to say so?

b*******s
发帖数: 5216
12
简单来说,
pf = f时
根据标准,函数对象f,被隐式转换成了指向函数对象的指针
然后才赋值给pf
而 pf = &f 不存在这个隐式转换
两者是等效的

【在 b*******s 的大作中提到】
: 是不一样的,有个隐式转换
: 你可以试试下面代码
: #include
: #include
: using namespace std;
: void f(int)
: {}
: void (*pf)(int);
: int main()
: {

b*******s
发帖数: 5216
13
被转换得到的是个纯右值,所以没什么副作用

【在 b*******s 的大作中提到】
: 简单来说,
: pf = f时
: 根据标准,函数对象f,被隐式转换成了指向函数对象的指针
: 然后才赋值给pf
: 而 pf = &f 不存在这个隐式转换
: 两者是等效的

b*******s
发帖数: 5216
14
你这个例子是误用
你对arr取地址得到的是指向int[3]对象的指针
和int *是不能implicitly转换的,当然也有workaround
不适用以下规则 (p = arr适用)
这个规则是array obj到指针的转换
4.2 Array-to-pointer conversion [conv.array]
1 An expression of type “array of N T”, “array of runtime bound of T”,
or “array of unknown bound of T” can
be converted to a prvalue of type “pointer to T”. The result is a pointer
to the ?rst element of the array.

【在 i**p 的大作中提到】
: int arr[] = {1, 2, 3};
: int *p;
: p = arr; // OK
: p = &arr; // error: cannot convert 'int (*)[3]' to 'int*' in assignment
: p = (int *)&arr; // OK
: So arr and &arr are different somehow.
:
: array
: .

f*******n
发帖数: 12623
15
还有
PtoF = *****subf;
你能understand吗?

【在 i**p 的大作中提到】
: void subf() {}
: void (*PtoF)(void);
: 1. PtoF = subf;
: 2. PtoF = &subf;
: 3. PtoF();
: 4. (*PtoF)();
: 1==2 hard to understand why it allows both
: 3==4 somehow easy to be understood
: Is it good to allow both?

i**p
发帖数: 902
16
ignored the post below, period.

【在 f*******n 的大作中提到】
: 还有
: PtoF = *****subf;
: 你能understand吗?

i**p
发帖数: 902
17
ignored the post below, period.

【在 l*********s 的大作中提到】
: sure, but why making a fuss over such a trifle. unless you are gathering
: opinion to design your own language, if that is case, hat off a salute!

f*******n
发帖数: 12623
18
你不想学习还来这里post干嘛?

【在 i**p 的大作中提到】
: ignored the post below, period.
i**p
发帖数: 902
19
ignored the post below, period.

【在 f*******n 的大作中提到】
: 你不想学习还来这里post干嘛?
b*******s
发帖数: 5216
20
这是cstyle

【在 f*******n 的大作中提到】
: 还有
: PtoF = *****subf;
: 你能understand吗?

相关主题
问题C++的"初始化"小结
[c++] reference 真得不能bound to a second object 么?c++如果调用没参数的函数不用加()就好了
c++ interview question为啥允许这样的const设计
进入Programming版参与讨论
i**p
发帖数: 902
21
void subf() {}
void (*PtoF)(void);
1. PtoF = subf;
2. PtoF = &subf;
3. PtoF();
4. (*PtoF)();
1==2 hard to understand why it allows both
3==4 somehow easy to be understood
Is it good to allow both?
d****i
发帖数: 4809
22
你的函数指针的定义不对,应该对应函数的argument, 比如
void subf(int a, void *ptr) {}
void (*PtoF)(int, void*);
至于下面两个为什么一样,函数指针用函数名赋值和用函数的地址是等效的,可以想象
成函数名就是函数的入口地址。
PtoF = subf;
PtoF = &subf;

【在 i**p 的大作中提到】
: void subf() {}
: void (*PtoF)(void);
: 1. PtoF = subf;
: 2. PtoF = &subf;
: 3. PtoF();
: 4. (*PtoF)();
: 1==2 hard to understand why it allows both
: 3==4 somehow easy to be understood
: Is it good to allow both?

i**p
发帖数: 902
23
changed the function's arguments. Simplified the code before published, but
not all cleaned.

【在 d****i 的大作中提到】
: 你的函数指针的定义不对,应该对应函数的argument, 比如
: void subf(int a, void *ptr) {}
: void (*PtoF)(int, void*);
: 至于下面两个为什么一样,函数指针用函数名赋值和用函数的地址是等效的,可以想象
: 成函数名就是函数的入口地址。
: PtoF = subf;
: PtoF = &subf;

i**p
发帖数: 902
24
"函数指针用函数名赋值和用函数的地址是等效的,可以想象成函数名就是函数的入口
地址。"
So it is not consistent to usage &i which is i's address.
Any reason not to disable &subf to keep & consistency?

【在 d****i 的大作中提到】
: 你的函数指针的定义不对,应该对应函数的argument, 比如
: void subf(int a, void *ptr) {}
: void (*PtoF)(int, void*);
: 至于下面两个为什么一样,函数指针用函数名赋值和用函数的地址是等效的,可以想象
: 成函数名就是函数的入口地址。
: PtoF = subf;
: PtoF = &subf;

d****i
发帖数: 4809
25
I don't know the exact reason why it allows both. But you can also use array
name as the address of the first element of array as an analogy, right?
After all, code segment lives in different address space than data segment.
Functions are different from variables.

【在 i**p 的大作中提到】
: "函数指针用函数名赋值和用函数的地址是等效的,可以想象成函数名就是函数的入口
: 地址。"
: So it is not consistent to usage &i which is i's address.
: Any reason not to disable &subf to keep & consistency?

i**p
发帖数: 902
26
int arr[] = {1, 2, 3};
int *p;
p = arr; // OK
p = &arr; // error: cannot convert 'int (*)[3]' to 'int*' in assignment
p = (int *)&arr; // OK
So arr and &arr are different somehow.

array
.

【在 d****i 的大作中提到】
: I don't know the exact reason why it allows both. But you can also use array
: name as the address of the first element of array as an analogy, right?
: After all, code segment lives in different address space than data segment.
: Functions are different from variables.

l*********s
发帖数: 5409
27
that is the semantics of the language, I guess you have to live with it or
write your own.
i**p
发帖数: 902
28
Though we have to live with it, we still can make comments on it, right?
Do you really think you are super to say so?

【在 l*********s 的大作中提到】
: that is the semantics of the language, I guess you have to live with it or
: write your own.

b*******s
发帖数: 5216
29
是不一样的,有个隐式转换
你可以试试下面代码
#include
#include
using namespace std;
void f(int)
{}
void (*pf)(int);
int main()
{
pf = f;
pf = &f;
cout << "f:" << typeid(f).name() << endl;
cout << "&f:" << typeid(&f).name() << endl;
}

【在 i**p 的大作中提到】
: Though we have to live with it, we still can make comments on it, right?
: Do you really think you are super to say so?

l*********s
发帖数: 5409
30
sure, but why making a fuss over such a trifle. unless you are gathering
opinion to design your own language, if that is case, hat off a salute!

【在 i**p 的大作中提到】
: Though we have to live with it, we still can make comments on it, right?
: Do you really think you are super to say so?

相关主题
C++中怎么传递std::hex这样的参数啊简单的c code问题
纯虚函数问题C puzzle 一日一题
int F::*x = &F::x是什么意思?问一个volatile和memcpy一起用的问题
进入Programming版参与讨论
b*******s
发帖数: 5216
31
4.3 Function-to-pointer conversion [conv.func]
1 An lvalue of function type T can be converted to a prvalue of type “
pointer to T.” The result is a pointer tothe function.57
I think it is what happend.

【在 i**p 的大作中提到】
: Though we have to live with it, we still can make comments on it, right?
: Do you really think you are super to say so?

b*******s
发帖数: 5216
32
简单来说,
pf = f时
根据标准,函数对象f,被隐式转换成了指向函数对象的指针
然后才赋值给pf
而 pf = &f 不存在这个隐式转换
两者是等效的

【在 b*******s 的大作中提到】
: 是不一样的,有个隐式转换
: 你可以试试下面代码
: #include
: #include
: using namespace std;
: void f(int)
: {}
: void (*pf)(int);
: int main()
: {

b*******s
发帖数: 5216
33
被转换得到的是个纯右值,所以没什么副作用

【在 b*******s 的大作中提到】
: 简单来说,
: pf = f时
: 根据标准,函数对象f,被隐式转换成了指向函数对象的指针
: 然后才赋值给pf
: 而 pf = &f 不存在这个隐式转换
: 两者是等效的

b*******s
发帖数: 5216
34
你这个例子是误用
你对arr取地址得到的是指向int[3]对象的指针
和int *是不能implicitly转换的,当然也有workaround
不适用以下规则 (p = arr适用)
这个规则是array obj到指针的转换
4.2 Array-to-pointer conversion [conv.array]
1 An expression of type “array of N T”, “array of runtime bound of T”,
or “array of unknown bound of T” can
be converted to a prvalue of type “pointer to T”. The result is a pointer
to the ?rst element of the array.

【在 i**p 的大作中提到】
: int arr[] = {1, 2, 3};
: int *p;
: p = arr; // OK
: p = &arr; // error: cannot convert 'int (*)[3]' to 'int*' in assignment
: p = (int *)&arr; // OK
: So arr and &arr are different somehow.
:
: array
: .

f*******n
发帖数: 12623
35
还有
PtoF = *****subf;
你能understand吗?

【在 i**p 的大作中提到】
: void subf() {}
: void (*PtoF)(void);
: 1. PtoF = subf;
: 2. PtoF = &subf;
: 3. PtoF();
: 4. (*PtoF)();
: 1==2 hard to understand why it allows both
: 3==4 somehow easy to be understood
: Is it good to allow both?

i**p
发帖数: 902
36
ignored the post below, period.

【在 f*******n 的大作中提到】
: 还有
: PtoF = *****subf;
: 你能understand吗?

i**p
发帖数: 902
37
ignored the post below, period.

【在 l*********s 的大作中提到】
: sure, but why making a fuss over such a trifle. unless you are gathering
: opinion to design your own language, if that is case, hat off a salute!

f*******n
发帖数: 12623
38
你不想学习还来这里post干嘛?

【在 i**p 的大作中提到】
: ignored the post below, period.
i**p
发帖数: 902
39
ignored the post below, period.

【在 f*******n 的大作中提到】
: 你不想学习还来这里post干嘛?
b*******s
发帖数: 5216
40
这是cstyle

【在 f*******n 的大作中提到】
: 还有
: PtoF = *****subf;
: 你能understand吗?

相关主题
问一个volatile和memcpy一起用的问题大家要学习C++11啊, 我觉得C++11很多FEATURE,绝对不输JAVA.
今天给c++震惊了[求教大虾]关于C++编译期变量和运行期变量的区别,总是有疑惑
*(&b1)=b编译不过,b1=b可以,区别是?问一个关于C++指针的问题
进入Programming版参与讨论
S*A
发帖数: 7142
41
C++ 我不熟悉啊,C 我还略知道一点。
这个相关的规定在 C99 6.6.9 Constant Expression 章节里面。
也就是说,地址表达式可以 “&” 取地址是显式取地址,
然后也可以对数组和函数隐含取地址,不需要 “&”。
数组和函数的地址是特例,取地址可以隐含,因为数组和函数的
本来使用都用特殊语法后缀,数组要后面有 【】,函数后面有()。
所以没有后缀的用法就可以分别定义。这里退化为地址是最自然的。
注意看原文里的 “implicitly" use an expression of array or function type.
9.
An address constant is a null pointer, a pointer to an lvalue designating an
object of static
storage duration, or a pointer to a function designator; it shall be created
explicitly using
the unary & operator or an integer constant cast to pointer type, or
implicitly by the use of
an expression of array or function type. The array-subscript [] and member-
access .
and -> operators, the address & and indirection * unary operators, and
pointer casts may
be used in the creation of an address constant, but the value of an object
shall not be
accessed by use of these operators.
w******w
发帖数: 126
42
确定这样的写法是用 C++? 如果是我我宁愿用 functor 取而代之.
如果你要多态的话, 再封装几个classes 吧! 既然你想用 C++ 的OO , 那么就把
C 的函数指针放弃吧.
1 (共1页)
进入Programming版参与讨论
相关主题
c++如果调用没参数的函数不用加()就好了今天给c++震惊了
为啥允许这样的const设计*(&b1)=b编译不过,b1=b可以,区别是?
C++中怎么传递std::hex这样的参数啊大家要学习C++11啊, 我觉得C++11很多FEATURE,绝对不输JAVA.
纯虚函数问题[求教大虾]关于C++编译期变量和运行期变量的区别,总是有疑惑
int F::*x = &F::x是什么意思?问一个关于C++指针的问题
简单的c code问题刚看完类这一章,有些大小问题,请指教,谢谢
C puzzle 一日一题关于placement new
问一个volatile和memcpy一起用的问题问题
相关话题的讨论汇总
话题: ptof话题: subf话题: pointer话题: array话题: int