I have the following structure in C++:
class A
{
protected:
int a;
};
class B : public A
{
public:
static void initThreads(); //Initialize threads (pthreads)
/* Threads */
static void* send(void*);
static void* receive(void*);
}
These functions “send” and “receive” operate on “a”. But a non-static member cannot be edited in a static function. Because send and receive are threads, hence they need to be defined as static. I would have only 1 object of class B, though there are multiple objects of class A.
So, is there a way to use “a” within “send” and “receive” in some manner?
Static member functions don’t work on non-static members because they don’t have an object attached to them; they are missing the implied
thispointer that gets passed invisibly to non-static member functions.The way around this is to explicitly pass a pointer to an object to work with. Your static function will have access to the private and protected members of the object, because it’s part of the class.