Class wrapper
-
Hi all, I've got a small problem with derived classes and a wrapper class. To show you what my problem is, I write down some code. Here's what I do now: class Base { doFunc1(); virtual doFunc2(); } class Der1 : public Base { doFunc2(); } class Der2 : public Base { doFunc2(); } class Wrap { Der1 *d1; Der2 *d2; doFunc1(); doFunc2(); } Wrap::doFunc1() { if(somecondition) d1->doFunc1(); else d2->doFunc1(); } Wrap::doFunc2() { if(somecondition) d1->doFunc2(); else d2->doFunc2(); } From my program I can call: Wrap::doFunc1(); Now I want to remove the Wrapper calls and replace them with function pointers like this: class Wrap { Der1 *d1; Der2 *d2; (*doFunc1)(); (*doFunc2)(); } Wrap::Wrap { if(somecondition) this->doFunc1 = &Der1::doFunc1;// causes C2440 this->doFunc2 = &Der1::doFunc2;// causes C2440 else this->doFunc1 = &Der1::doFunc1;// causes C2440 this->doFunc2 = &Der1::doFunc2;// causes C2440 } From my program I would like to call: Wrap::doFunc1(); The line this->doFunc1 = ... causes C2440: type cast: cannot convert from (__thiscall Der1::*)() to (__cdecl*)(). I know that class members are not the same as normal c-functions, this causes the __thiscall to __cdecl error, but I thought that there is some trick to get it to work. Any ideas? jung-kreidler
-
Hi all, I've got a small problem with derived classes and a wrapper class. To show you what my problem is, I write down some code. Here's what I do now: class Base { doFunc1(); virtual doFunc2(); } class Der1 : public Base { doFunc2(); } class Der2 : public Base { doFunc2(); } class Wrap { Der1 *d1; Der2 *d2; doFunc1(); doFunc2(); } Wrap::doFunc1() { if(somecondition) d1->doFunc1(); else d2->doFunc1(); } Wrap::doFunc2() { if(somecondition) d1->doFunc2(); else d2->doFunc2(); } From my program I can call: Wrap::doFunc1(); Now I want to remove the Wrapper calls and replace them with function pointers like this: class Wrap { Der1 *d1; Der2 *d2; (*doFunc1)(); (*doFunc2)(); } Wrap::Wrap { if(somecondition) this->doFunc1 = &Der1::doFunc1;// causes C2440 this->doFunc2 = &Der1::doFunc2;// causes C2440 else this->doFunc1 = &Der1::doFunc1;// causes C2440 this->doFunc2 = &Der1::doFunc2;// causes C2440 } From my program I would like to call: Wrap::doFunc1(); The line this->doFunc1 = ... causes C2440: type cast: cannot convert from (__thiscall Der1::*)() to (__cdecl*)(). I know that class members are not the same as normal c-functions, this causes the __thiscall to __cdecl error, but I thought that there is some trick to get it to work. Any ideas? jung-kreidler
Look at the pointer-to-member operators
.*
and->*
«_Superman_»
-
Look at the pointer-to-member operators
.*
and->*
«_Superman_»
Does not help, since my Wrapper is a different class. Pointer-to-member works only inside a class, e.g. Testpm: void (Testpm::*pmfn)() = &Testpm::m_func1;. I need e.g. void (Wrap::*doFunc1)() = &Der1::doFunc1. Using it on different classes causes C2440 :( Thanks for the answer. :-D