473,388 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

pure virtual static function :/

emibt08
25
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(LPVOID 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
Jan 20 '09 #1
4 4214
newb16
687 512MB
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

Expand|Select|Wrap|Line Numbers
  1. class abstr
  2. {
  3. public:
  4.   virtual void worker() = 0;
  5.   static void workStatic(void*self);
  6. };
  7.  
  8. void abstr::workStatic(void*self)
  9. {
  10.   abstr *me = reinterpret_cast<abstr*>(self);
  11.   me->worker();
  12. }
  13.  
  14. class foo:public abstr
  15. {
  16. public:
  17.   foo() {}
  18.   void worker()
  19.   { 
  20.     printf("do stuff\n");
  21.   }  
  22. };
  23.  
  24. int main()
  25. {
  26.   foo* my = new foo();
  27.   abstr::workStatic( static_cast<abstr*>(my) );
  28. }
  29.  
As I understand, it's important to cast to
static_cast<abstr*>(my)
first, and then to void*, else it will be incorrectly reinterpret_cast'ed back.
Jan 20 '09 #2
emibt08
25
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:

Expand|Select|Wrap|Line Numbers
  1. class CAbstract
  2. {
  3.     //...
  4. protected:
  5.     virtual int Calculate(int nID, int nVal) = 0;
  6.     //...
  7. }
  8.  
  9. class CDerived : public CAbstract
  10. {
  11.     //...
  12. public:
  13.     virtual int Calculate(int nID, int nVal);
  14. private:
  15.     static DWORD WINAPI WorkerThreadProc(LPVOID lpParameter);
  16.     //...
  17. }
  18.  
  19. int CDerived::Calculate(int nID, int nVal)
  20. {
  21.     //...
  22.     CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&WorkerThreadProc, (LPVOID)pParams, 0, NULL);
  23.     //
  24.     // pParams is an object that i create before creating the thread.
  25.     // It contains the 'this' pointer along with some data needed for calculation
  26.     //...
  27. }
  28.  
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 WorkerThreadProc(), 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 WorkerThreadProc() 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
Jan 21 '09 #3
newb16
687 512MB
In this case, when you don't implement WorkerThreadProc 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 WorkerThreadProc 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 WorkerThreadProc 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?
Jan 21 '09 #4
weaknessforcats
9,208 Expert Mod 8TB
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::LaunchThread. 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::ExecuteThread. 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.
Jan 21 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
5
by: Ruben Campos | last post by:
I've recently noticed that it's not allowed to call a pure (non-implemented) virtual method inside a constructor or a destructor, doesn't matter if this method is declared in the considered class...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
22
by: ypjofficial | last post by:
Hello All, I have following doubt.. class abstractclass { public: abstractclass(){} virtual void method()=0; };
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
10
by: Rahul | last post by:
Hi, I tried to create a abstract class by having a non-virtual member function as pure, but i got a compilation error saying "only virtual member functions can be pure"... I'm trying to think...
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.