1

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

Example:

typedef struct
{
    int func(int z)
    {
        return z * 2;
    }
} sta;

typedef struct
{
    int(*this.func)(int);
} stah;


int main()
{
    sta sa;
    stah sah;

    sah.func = &sa.func;

    return 0;
}

it's possible this in a struct?

3
  • In your example you're not using anonimous struct, because you giving them name Commented Oct 21, 2016 at 9:17
  • @DenisSheremet my bad. Commented Oct 21, 2016 at 10:31
  • Here is possible solution Commented Oct 21, 2016 at 10:36

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

11 Comments

the problem is that sta is imaginary, how would be in memory address?
there any form?
@nikomaster sta is a well defined class in your example. What does it mean is imaginary??
i means for reverse engineering, example:
@nikomaster No, if you want use a pointer to function instead of a pointer to member function, you must define your member methods as static. As an example, see here.
|
1

The correct syntax for a pointer to method is:

&T::f

Where T is a type declaring a method f. Note that to be called, the pointer must be bound to an instance of T, because the value of the pointer represents an offset to the beginning of an instance in memory.

In C++14, you may consider std::function:

#include <functional>

struct sta
{
    int func(int z)
    {
        return z * 2;
    }
};

struct stah
{
    std::function<int(int)> func;
};


int main()
{
    sta sa;
    stah sah;

    sah.func = std::bind(&sta::func, &sa, std::placeholders::_1);

    return 0;
}

You can also use lambdas instead of std::bind:

int main()
{
    sta sa;
    stah sah;

    sah.func = [&sa](int z) { return sa.func(z); };

    return 0;
}

See std::function, std::bind, and std::placeholders on cppreference.com.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.