pure virtual static function :/

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emibt08
    New Member
    • Oct 2008
    • 25

    pure virtual static function :/

    Hi.
    I know the title looks a little bit silly and that we can not have pure virtual static functions. But i've been wondering what approach to take to make the abstraction.
    This is the case: I have an abstract class with one pure virual function. Now, all of the classes derived from the base abstract class have a ThreadProc function, for the job that the worker threads do. They must have it because of the nature of the abstract class. So, in all of them i would make a function like:
    static DWORD WINAPI ThreadProc(LPVO ID lpParameter);

    It's all fine, and of course it works. But i just want to make sure that all of the derived classes will have that function implemented. Now, that's what the pure virtual functions are for, but since the function must be static, it can not be a pure virtual AND static at the same time.
    Please give me any tips on this one.

    Cheers
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    Is ThreadProc is the pure virtual function in base abstract class, and LPVOID lpParameter takes actually (this) pointer to the class itself?
    I would try like this

    Code:
    class abstr
    {
    public:
      virtual void worker() = 0;
      static void workStatic(void*self);
    };
    
    void abstr::workStatic(void*self)
    {
      abstr *me = reinterpret_cast<abstr*>(self);
      me->worker();
    }
    
    class foo:public abstr
    {
    public:
      foo() {}
      void worker()
      { 
        printf("do stuff\n");
      }  
    };
    
    int main()
    {
      foo* my = new foo();
      abstr::workStatic( static_cast<abstr*>(my) );
    }
    As I understand, it's important to cast to
    static_cast<abs tr*>(my)
    first, and then to void*, else it will be incorrectly reinterpret_cas t'ed back.

    Comment

    • emibt08
      New Member
      • Oct 2008
      • 25

      #3
      Thanks for the suggestion newb16. However, it won't work for me. I am sorry for not being clear enough. Well, this is how actually the scenario looks like:

      Code:
      class CAbstract
      {
      	//...
      protected:
      	virtual int Calculate(int nID, int nVal) = 0;
      	//...
      }
      
      class CDerived : public CAbstract
      {
      	//...
      public:
      	virtual int Calculate(int nID, int nVal);
      private:
      	static DWORD WINAPI WorkerThreadProc(LPVOID lpParameter);
      	//...
      }
      
      int CDerived::Calculate(int nID, int nVal)
      {
      	//...
      	CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&WorkerThreadProc, (LPVOID)pParams, 0, NULL);
      	//
      	// pParams is an object that i create before creating the thread.
      	// It contains the 'this' pointer along with some data needed for calculation
      	//...
      }
      As you can see, i have the pure virtual function Calculate(), which of course, is implemented in all of the derived classes. Now, Calculate() depends on WorkerThreadPro c(), so i have it in all derived classes as well. Alse, as you can see Calculate() is the only public function here. Now, i somehow want to impose all of the derived classes to have WorkerThreadPro c() and they just implement it as needed. Since it can not be implemented in the class CAbstract, i kind of need another solution. I know it's nothing too important, after all, i'll implement it in all of the derived classes anyway and it will work, but i just feel it lacks something. Having to have the same function in all of the derived classes, with the same name and parameters, and still not mentioned in the abstract class. Maybe i am too picky, but i don't really like that idea.
      So, if anyone has a suggestion, please let me know :)

      Cheers

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        In this case, when you don't implement WorkerThreadPro c in derived class, you will get compilation error anyway, as if you didn't implement abstract function in some derived class. If it's impossible to unify WorkerThreadPro c so that it can be implemented as static in basic class, calling abstract virtual function of derived, (like in my first reply) your solution seems be working.

        But why are you using pparams in call to CreateThread? If there is some class-related data, you can probably move pparams' content to class members, and make WorkerThreadPro c with functionality like in my first reply (and make them both protected, if it matters). If not, why is it a class member, albeit static?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          I beloeve you are on the wrong approach here.

          First, your thread must be a THREADPROC, that is a function that takes an LPVOID and returns a DWORD.

          Second, you write a member function maybe called MyClass::Launch Thread. This function passes the this pointer to CreateThread plus the address of a static member function that is your THREADPROC.

          Third, you write a static member function as your THREADPROC. Here you takes the LPVOID argument and typecast it back to a MyClass pointer. This static function is your thread. When it completes, your thread is done.

          Fourth, in the static member funciton and using the typecast pointer call another class method MyClass::Execut eThread. From here ExecuteThread can call any other class methods as necesssary. When it returns, the static function returns and the thread is done.

          You should not have any virtual problems since multuthreading and polymorphism are independednt of each other.

          Comment

          Working...