ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello,
Do you know how can I call a non-static member of a class from a static member of the same class. For example:
class Entry
{
private:
static void * fun();
public:
int getSomeThing()
}
void * Entry::fun()
{
//code...
i = getSomeThing();
return NULL;
}
The compiler complains about this and says that I cannot call a static member from a non-static member. Is there any way to solve this problem without changing the structure of the program?. The fun() function has to be static for other reasons.
Well, a static class member function can be called without the prescence of an object of the class. Thus it needs an object if it wants to access something not static in the class.
The static member can't call a non static member directly, only via an object of that class. So you would either need to pass the object into the static member, create an instance within the method itself or make the non static member static. The reverse is not the case that is a non static member can call a static member.
This is because the static members belong to the class definition whilst the non static members belong to the object, and thus have access to the member variables.
Hello again Graeme,
I will try to explain to you what I'm trying to do here.
class Power
{
private:
static void * update();
Timer* t; //timer will start a new thread with the function I pass to the constructor
public:
int getIpAddress()
}
void * Power::update()
{
//code...
i = getIpAddress();
return NULL;
}
Power::Power()
{
//this is what i'm trying to do.
//the Timer class starts its own thread and run update every few seconds
t = new Timer(update);
//code....
}
In order to do this i have to declare update to be static so i can pass its address to Timer, but then i cannot call getIpAddress since update() is a static. What can i do to fix this problem??
Why do you say that the function being passed needs to be static? Is that how you declared it in Timer?
Function pointers can be either static or non-static however the way they are implemented is slightly different.
I suggest that you change your Timer constructor. But do you really need to send a pointer function? If it will always run the update method then just pass a pointer to the object and then call update directly.
Thanks Graeme,
I solve the problem by adding a class with a virtual update() called updatable, and will be the base class for Power.
This way i can get late binding. Also, I added a different constructor in Timer. update had to be static because pthread_create in Timer only accept static.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.