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
4 4240
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 -
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.
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: -
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
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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.
|
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.
|
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
|
by: ypjofficial |
last post by:
Hello All,
I have following doubt..
class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};
| |
by: sks |
last post by:
Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?
|
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
|
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.
|
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>
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |