Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Class wrapper

Class wrapper

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jung kreidler
    wrote on last edited by
    #1

    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

    _ 1 Reply Last reply
    0
    • J 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

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Look at the pointer-to-member operators .* and ->*

      «_Superman_»

      J 1 Reply Last reply
      0
      • _ _Superman_

        Look at the pointer-to-member operators .* and ->*

        «_Superman_»

        J Offline
        J Offline
        jung kreidler
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups