关于类成员函数指针的sizeof [英] About sizeof of a class member function pointer

查看:115
本文介绍了关于类成员函数指针的sizeof的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有A类

class A;

和这些typedefs

and these typedefs

typedef void (A::*a_func_ptr)(void);
typedef void (*func_ptr)(void);

我的问题是为什么sizeof(a_func_ptr)返回16,而sizeof(func_ptr)返回4(对于x86系统上的任何指针)?

My question is why sizeof(a_func_ptr) returns 16, while sizeof(func_ptr) returns 4 (as for any pointer on x86 system)?

例如

int main(int argc, char *argv[])
{
  int a = sizeof(a_func_ptr);
  int b = sizeof(func_ptr);
}

推荐答案

我的问题是为什么sizeof(a_func_ptr)返回16,而sizeof(func_ptr)返回4(对于x86系统上的任何指针)?

My question is why sizeof(a_func_ptr) returns 16, while sizeof(func_ptr) returns 4 (as for any pointer on x86 system)?

因为指向成员的指针的实现方式不同.它们是引擎盖下的 not 指针.某些编译器(例如MSVC)将它们实现为struct,其中包含多个成员.

Because pointer-to-members are implemented differently. They're not pointers under the hood. Some compilers, such as MSVC, implement them as struct with more than one members in it.

阅读这篇有趣的文章:

请注意,在某些编译器中,它们的大小可能相同.底线是:它们依赖于编译器.

Note that in some compilers, they might have same size. The bottomline is: they're compiler-dependent.

这篇关于关于类成员函数指针的sizeof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆