Search code examples
c++classpointerssizeofmember

About sizeof of a class member function pointer


Let's say we have a class A

class A;

and these typedefs

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

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

For instance

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

Solution

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

    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.

    Read this interesting article:

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