struct pointer function points to other function of other struct

后端 未结 3 1643
天命终不由人
天命终不由人 2021-01-24 16:27

I was wondering if is possible to point a function of other structure into of a structure:

Example:

typedef struct
{
    int func(int z)
    {
        r         


        
3条回答
  •  死守一世寂寞
    2021-01-24 17:15

    The declaration of func should look like this:

    int(sta::*func)(int);
    

    Or, alternatively:

    using my_type = int(sta::*)(int);
    my_type func;
    

    This is easier to read: my_type is an alias for the type pointer to a member function of sta that gets an int and returns an int.
    func is nothing more that a data member having type my_type.

    In order to assign an actual pointer to member function to func, you can do this instead:

    sah.func = &sta::func;
    

    You can then invoke it as it follows:

    (sa.*sah.func)(0);
    

提交回复
热议问题