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. Function Pointer or Class Pointer ?

Function Pointer or Class Pointer ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
2 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.
  • M Offline
    M Offline
    Mr Brainley
    wrote on last edited by
    #1

    I have a class containing these methods/members :

    class CWorkerThread
    {
    public:
    CWorkerThread(const HANDLE* hJobSignal); // creates Win32-Thread
    ~CWorkerThread();

    private:
    void run(); // member-function that does the actual work
    static DWORD WINAPI StaticThreadFunction(LPVOID lpParameter); // entry-point for Win32-Threads

    const HANDLE\*	m\_hJobSignal;
    

    };

    The constructor creates a Thread and passes it the StaticThreadFunction to execute. As parameter it can now pass either a pointer to run(), or a the this-pointer. Wich is better ? I really see no big difference, but i fear i might miss some important point here. Thanks !

    B 1 Reply Last reply
    0
    • M Mr Brainley

      I have a class containing these methods/members :

      class CWorkerThread
      {
      public:
      CWorkerThread(const HANDLE* hJobSignal); // creates Win32-Thread
      ~CWorkerThread();

      private:
      void run(); // member-function that does the actual work
      static DWORD WINAPI StaticThreadFunction(LPVOID lpParameter); // entry-point for Win32-Threads

      const HANDLE\*	m\_hJobSignal;
      

      };

      The constructor creates a Thread and passes it the StaticThreadFunction to execute. As parameter it can now pass either a pointer to run(), or a the this-pointer. Wich is better ? I really see no big difference, but i fear i might miss some important point here. Thanks !

      B Offline
      B Offline
      Blake Miller
      wrote on last edited by
      #2

      I favor passing a pointer to 'this'. Inside the static function, you cast the LPVOID to a 'this' pointer and then call the virtual function 'Run'. Therefore, if someone dervies from your thread class, the static function will always call the 'Run' of the derived class. I am not sure of the behavior if you pass in the Run pointer directly, from the base class, not sure you would get the Run of the dervied class to execute. My static thread runner functions typically have two lines in them: DWORD WINAPI CWorkerThread::StaticThreadFunction(LPVOID lpParameter) { CWorkerThread* pRunner = (CWorkerThread*)lpParameter; return pRunner->Run(); } It really is that simple!

      Any sufficiently gross incompetence is nearly indistinguishable from malice.

      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