3

I was wondering if is possible get pointer that points a method pointer of a class, i've tried something like this, consider this:

class x {
    public:
        int a;
        int GetSomeVal(int b) {
            return b * 4;
        }
};
class y {
    public:
        int ha;
        int (*HGetSomeVal)(int); // pointer method
};

// y::HGetSomeVal now points to x::GetSomeVal
int(y::*HGetSomeVal)(int) = (int(y::*)(int))&x::GetSomeVal;

Used in this context:

int main() {
    x a;
    y* b;
    b = (y*)&a; // pointer of x to y

    std::function<int(int)> NewFunc;
    std::function<int(int)> OtherNewFunc;
    NewFunc = std::bind1st(std::mem_fun(&x::GetSomeVal), b); // works!
    OtherNewFunc = std::bind1st(std::mem_func(&y::*HGetSomeVal), b); // not get the pointer, i want get the pointer that points  to x::GetSomeVal through of y::HGetSomeVal.

    // print testing pointers
    printf("%p | %p", &x::GetSomeVal, &y::*HGetSomeVal);
    // Pointer A: 00421e00 SUCESS, Pointer B: 000000 NOT GET POINTER.


    return 0;
}

Duplicate edit: this isn't duplicated, i've followed Function pointer to member function

and isn't same i'm using a class pointer, then i've done something like this:

OtherNewFunc = std::bind1st(std::mem_func((y->*(y->HGetSomeVal)), b);

printf("Pointer A:  %p | \n", (y->*(y->HGetSomeVal)));

then i'm getting the next error:

internal compiler error: in gimplify_expr, at gimplify.c:8343

How i can get the value of pointer that points HGetSomeVal?

Community
  • 1
  • 1
MindLerp
  • 378
  • 1
  • 3
  • 15
  • 2
    What are you trying to do with `b = (y*)&a;`? With just that much it already looks like a terrible idea. – aschepler Nov 08 '16 at 20:30
  • `int (*HGetSomeVal)(int); // pointer method` fails you right away because it doesn't know how to handle `this` of `x`. You shoehorn it with the `(int(y::*)(int))` cast in `int(y::HGetSomeVal)(int) = (int(y::*)(int))&x::GetSomeVal;`, but this will only hold so long as you don't use `this`. At that point you may as well go `static`. – user4581301 Nov 08 '16 at 20:38
  • There is no such thing as "pointer method". `HGetSomeVal` is a class member if type `int (*)(int)` (a pointer to a function of type `int(int)`). `GetSomeVal` is not of type `int(int)`, `HGetSomeVal` cannot point to it. – n. m. could be an AI Nov 08 '16 at 20:39
  • Give this a read through: [Pointers to Member Functions](https://isocpp.org/wiki/faq/pointers-to-members) – user4581301 Nov 08 '16 at 20:41
  • 1
    "internal compiler error" is not your fault, it is a compiler bug. However hitting one normally means you are trying to do something very unusual or complicated. – n. m. could be an AI Nov 08 '16 at 20:41
  • Last but not least, `std::bind1st` and `std::mem_fun` are deprecated. – n. m. could be an AI Nov 08 '16 at 20:44

0 Answers0