struct pointer function points to other function of other struct

后端 未结 3 1637
天命终不由人
天命终不由人 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:11

    After trying and trying, the solution it was something like this:

    Example:

    typedef struct 
    {
        int a;
    
        int SomeFunc(int a)
        {
            return a * 4;
        }
    
    } somst;
    
    
    typedef struct
    {
        int a;
        int (*HGetValX)(int);
    } hst;
    
    
    int main()
    {
        hst* a;
        hst decfunc; // New instance
        somst b;
    
        decfunc.HGetValX = (int(*)(int))0x421C10; // memory address, or &b.SomeFunc; | &b.SomeFunc; Produces warnings.
        b.a = 20;
    
        a = (hst*)&b;
    
    
        cout << decfunc.HGetValX(4) << b.SomeFunc(4) << a->a <<  endl;
    
        return 0;
    }
    

    Finding memory address

    then the code compiles without warnings, the objective is hook the structure with their functions.

提交回复
热议问题