c和c++中函数指针之间的差异
difference between function pointers in c and c++
可能重复:
如何使用cout打印函数指针?
c和c++中的函数指针有什么区别?当我在c++中打印函数指针时,它给出的是1,但在c中,它给出了一个地址。
#include <iostream>
int fun()
{}
typedef int (*f)();
int main()
{
f test = fun;
std::cout << reinterpret_cast<f>(test);
}
#include <stdio.h>
int fun()
{}
int (*f)();
int main()
{
f = fun;
printf("%p", f);
}
函数指针无法转换为void*
,因此在使用C++i/o的程序中,它会转换为bool
。C程序也是一个C++程序。当您将它编译为C++时,应该不会有任何区别。
干杯&hth。,
主要区别在于您使用的打印机制。std::cout和printf有不同的语义。std::cout将指针称为布尔数据,并打印0或1而不是地址。
真正的问题是,当您流式传输指向函数的指针时,带有"/p"的printf和cout之间有什么区别。
要在C++中打印出指向函数的指针,请尝试将其强制转换为char*并遍历字节(长度为sizeof(test((。我想你会发现它类似于"%p">
相关文章:
- 在 const 函数中通过引用和指针返回之间的区别
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- AVX 指令中寄存器和指针之间的客观差异
- 禁止指针和整数之间的比较C++
- 为什么不同类型层次结构的指针之间的dynamic_cast定义得很好?
- 介于 [固定数组] 和 [带内存分配的指针] 之间的性能
- ISO C++禁止指针和整数 [-fpermissive] [c++] 之间的比较
- 类指针之间的 Qt 差异
- 如何使用静态多态性在 int 和指针类型之间进行转换?
- 指向 POD 类型的指针之间的静态转换与重新解释转换
- 在两个.cpp文件之间定义全局类/结构指针
- 按值和引用传递指针之间的区别?
- 在硬件SIMD矢量指针和相应类型之间进行"interpret_cast"是一种未定义的行为吗
- 在C++中的程序之间共享指针
- 指针和程序性能之间有什么关系吗?
- Qt 错误 iso c++ 禁止指针和整数之间的比较 -permissive
- 可以在指向不同模板类实例的指针之间进行强制转换吗
- C++17:unique_ptr<char[]> 和 shared_ptr<char[] 之间的指针存储差异>
- 错误:ISO C++禁止在指针和整数 [-fallowive] 之间进行比较