2

Hi I was wondering if someone could explain to me a field in a struct that looks like this:

struct example {
void (SomeClass::*someMethod)();
};

What exactly is this and how/why would you use it? Thanks.

zProgrammer
  • 727
  • 4
  • 10
  • 22

2 Answers2

5

This structure contains a pointer to a function with void return type and without parameters.

We would set this pointer to an address of actual function and execute the function via the pointer some time later. Function pointers are very convenient thing for providing different functions to handle some task depending on circumstances.

Alexander V
  • 8,351
  • 4
  • 38
  • 47
  • Why is this pointer to a function is in a struct? What's the point in encapsulating this pointer in a struct? Wouldn't it be the same without the struct encapsulating the pointer? – Varaquilex Nov 14 '14 at 04:05
  • It looks like a pointer to a method/member function, not ordinary function. – Scooter Nov 14 '14 at 04:09
1
void (SomeClass::*someMethod)();

This is declaration of pointer to member function of "SomeClass" class which is accepting and returning void.

ravi
  • 10,994
  • 1
  • 18
  • 36