473,769 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pure virtual static function :/

emibt08
25 New Member
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
Jan 20 '09 #1
4 4240
newb16
687 Contributor
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<abs tr*>(my)
first, and then to void*, else it will be incorrectly reinterpret_cas t'ed back.
Jan 20 '09 #2
emibt08
25 New Member
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 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
Jan 21 '09 #3
newb16
687 Contributor
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?
Jan 21 '09 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Jan 21 '09 #5

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

Similar topics

12
13468
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 of your Derived classes will need to implement some method, but implement it differently, and that the base class cannot implement it. This is where pure virtual comes in.
5
2351
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 itself or in one of its base classes. Such an attempt results in a linker undefined symbol error. Why? Is it right, or is it a bad issue of my compiler/linker (I'm working with Microsoft Visual C++ 7.1.3088)? Thank you in advance for your help.
11
4368
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 it inline. Like this.
6
3491
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
2413
by: ypjofficial | last post by:
Hello All, I have following doubt.. class abstractclass { public: abstractclass(){} virtual void method()=0; };
21
2579
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
10
4805
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 pure virtual function body
10
2096
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 the reason behind this restriction... i just want to want a base class to be abstract so as to avoid object slicing into the base type from derived type, and in my case base class doesn't need to have a virtual function.
14
2757
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
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.