Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: MyndFyre on October 12, 2004, 07:56 AM

Title: Pointers to Member Functions
Post by: MyndFyre on October 12, 2004, 07:56 AM
Let's say I have the class definition:


class MyType
{
    public:
      doSomething(int nNumber, char *szText);
}


I want to create the typedef to have a pointer to that function.  However, that function requires use of the this pointer.  Where is the this pointer situated, and does it depend on the calling convention?


typedef (* pDoSomethingFunc)(int, char*, MyType*) PDSFUNC;
typedef (* pDoSomethingFunc)(MyType*, int, char*) PDSFUNC;


Or is it something else?
Title: Re: Pointers to Member Functions
Post by: K on October 12, 2004, 04:23 PM
AFAIK you can't really have a pointer to a non-static member function; the compiler won't let you.   If you want to try to hack it, though, member functions using thiscall pass the pointer in ecx and member functions using the c calling convention (which you really only need on member functions if you want to use varargs) pass it as an extra parameter.
Title: Re: Pointers to Member Functions
Post by: Yoni on October 12, 2004, 04:28 PM

class MyType
{
    public:
        int doSomething(int nNumber, char *szText);
};

typedef int (MyType::* PDSFUNC)(int, char*);

...

MyType* bleh;
PDSFUNC p;
int a;
char* b;

...

(bleh->*p)(a, b);

I keep this in Misc.txt because it's hard to remember. Enjoy. :)
Title: Re: Pointers to Member Functions
Post by: MyndFyre on October 12, 2004, 05:42 PM
Thanks Yoni!