- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有两个问题:
Q1) 函数名本身是指针吗??
如果它们是指针,那么它们存储的是什么值?
否则如果它们不是指针,那么,它们是什么以及其中存储了哪些值?
如果我们认为函数名是指针。然后:
void display(){...}
int main ()
{
void (*p)();
**p=display; //Works (justified**, because we are assigning one pointer into another)
**p=&display; //Works (Not justified** If function name is a pointer (let say type*) , then &display is of datatype : type**. Then how can we assign type** (i.e. &display) into type * (i.e. p)??)
**p=*display; //Works (Not justified** If function name is a pointer ( type *) ,then, how can we assign type (i.e. *display) into type * (i.e. p) ?? )
}
再次,
cout<<display<<";"<<&display<<";"<<*display;
打印类似的东西:
0x1234;0x1234;0x1234
[1234只是举例]
[天哪!这怎么可能?指针的地址、它指向的地址和指向地址的值怎么可能都相同?]
Q2) 用户定义的函数指针中存储了什么值?
考虑这个例子:
void display(){...}
int main()
{
void (*f)();
f=display;
f=*f; // Why does it work?? How can we assign type (i.e. *f ) into type * (i.e. f).
cout<<f<<";"<<&f<<";"<<*f;
//Prints something like :
0x1234;0x6789;0x1234
}
[前两个输出是合理的...但是指针(它指向的地址)中的值如何等于指向地址中存储的值?]
再次:
f=*********f; // How can this work?
我在网上搜索过,但所有可用的信息都是关于用法和创建函数指针的示例代码。它们都没有说明它们是什么或它们与普通指针有何不同。
所以我一定遗漏了一些非常基本的东西。请指出我所缺少的。 (抱歉我是初学者的无知。)
最佳答案
函数名称本身是指针吗?
没有。但是,在某些情况下,函数可以自动转换为指向函数的指针。该标准在第 4.3 段中说:
An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function.
(函数名指定一个左值,但可以有其他函数类型的左值)。
在你的例子中
p = display;
p = *p;
就是有这种自动转换。 display
和 *p
是函数类型的左值,在需要时,它们会自动自动转换为指向函数的指针类型。
p = *display;
此处转换发生两次:第一次display
转换为 *
的指针运算符,然后它被取消引用,然后再次转换为 =
的指针运营商。
cout << display << ";" << &display << ";" << *display;
在这里,display
转换为 operator <<
的指针; &display
已经是一个指针,因为 &
是一个正常的地址获取运算符(operator);和 *display
转换为 operator <<
的指针在里面display
转换为 operator *
的指针.
f = *********f;
这个表达式中有很多这样的转换。自己数一数!
关于c++ - C++中的函数指针歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41577136/
虽然我正在运行以测试此 thread 中的一些代码我发现了一件奇怪的事情,如果你考虑下面的程序 import java.util.ArrayList; import java.util.List; p
我只是很好奇为什么使用 using 指令以这种方式设计它。对于 1) struct 被视为命名空间,对于 2) 它不是: struct foo { using type0 = int; }; na
我需要在 ANTLR 中匹配一条消息,该消息包含由/分隔的 2 个字段第一个字段可以有 1..3 数字,第二个字段可以有 1..2 数字, 这不起作用 msg: f1 '/' f2; f1: DIGI
我想定义一个带有 x, y, z 坐标的 3D 矢量。可以使用(+)运算符添加向量,并可以使用length函数计算长度 如果我想编译它,我会收到以下错误: It could refer to eith
所以,今天我一直在测试 Java 的重载技术,但遇到了我无法解释的歧义。基本上,当存在带有原语及其相应包装器的可变参数方法时,编译器会提示并且无法决定选择哪一个,我不明白为什么?人类很容易决定而不是编
发现: Ambiguity while overloading the cast operator 和 C++ Operator Ambiguity 没有帮助 情况: 在容器类的内部我有: opera
我正在使用以下 XPath 片段 ancestor::contribution[1]/preceding-sibling::contribution[@speaker-reference][1] 我的
在尝试依赖可变参数模板实现一些事情时,我偶然发现了一些我无法解释的事情。我将问题归结为以下代码片段: template struct A {}; template class Z, typenam
我最近对 LALR 的研究足以写一篇 LALR generator ,并且我正在尝试为其构造一个 java 或 c# 风格的语法(其开头指定为 here )。 我知道编写解析器生成器需要额外的努力
在阅读 wikipedia article关于修订控制的文章,我发现了一些似乎定义不明确的术语,并且想知道它们是如何在现实世界中实际使用/应用的。具体来说: “主线”与“基线” “分支”与“流” “结
我有以下代码: class A{ public void print(A a){ System.out.println("A 1"); } } class B exte
考虑我试过的以下查询,有两个表,Orders 和 Customers,每个表都有列名称 CustomerID,当我尝试显示两个 CustomerID 时,只有一列显示,我不明白为什么会这样,或者我是理
我正在为基本汇编语言编写一个简单的组合词法分析器/解析器。我的问题是,在解析操作码时,我需要解析一些十六进制,它是指令计数器,可能是立即值等,以及实际的操作码。 基本上,当解析像 add 这样的东西时
我正在尝试在 mysql 中跨两个不同的数据库进行标题搜索,以匹配来自不同来源的数据。在 db1 或 db2 中,标题有时会在一个数据库中以“第一个标题”开头,而在另一个数据库中则以“第一个标题”开头
我对 token 级别的歧义有疑问。 问题看起来是这样的。我的代码看起来像这样,因此标记 t_UN1 具有更高的优先级。 t_ignore = ' \t\v\r' # whitespace ....
在 Main() 的最后一行,我在尝试执行指针操作时遇到错误,但我实际上想做的是调用我在 Foo 类中定义的乘法运算符。如果这不是调用乘法的语法,那是什么? namespace test {
在此page它说: The "00" specifier causes the value to be rounded to the nearest digit preceding the decim
我正在使用 C++ Builder 并使用 TDateTime 数据类型创建 GUI 应用程序。 当我在 TDateTime 对象上使用 MonthOf() 方法时,我得到的值比 TDateTime
$7.3.3/14 (C++03) struct A { int x(); }; struct B : A { }; struct C : A { using A::x; int x(in
我一直在开发一种适配器类,当我在 clang 下遇到问题。当定义了左值引用和右值引用的转换运算符时,您会在尝试从您的类中移出时遇到歧义编译错误(当这样的代码应该没问题时,如 operator cons
我是一名优秀的程序员,十分优秀!