LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   static member and non-static member (https://www.linuxquestions.org/questions/programming-9/static-member-and-non-static-member-399953/)

allomeen 01-05-2006 03:59 PM

static member and non-static member
 
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.


Thanks,
Alaa

Hivemind 01-05-2006 04:04 PM

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.

dmail 01-05-2006 04:04 PM

maybe by passing a pointer/reference to the class which you want to call as the parameter to the static func, or maybe you need to rethink the class.

graemef 01-05-2006 04:39 PM

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.

graeme.

allomeen 01-05-2006 04:50 PM

Thanks guys for the help. But how would you pass the object to the static member since the static function belongs to that object?

Alaa

graemef 01-05-2006 05:13 PM

you would need to define your static function as follows:

Code:

static void * fun(Entry obj);
and then call it as follows:

Code:

Entry obj;
Entry::fun(obj);

Which kind of defeats the purpose of the function being static but then you never asked for a reasonable solution ;)

graeme.

allomeen 01-05-2006 05:50 PM

Hello graeme,
How else would you do it?

Alaa

allomeen 01-05-2006 06:14 PM

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??

Thanks,
Alaa

graemef 01-05-2006 06:54 PM

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.

graeme.

allomeen 01-06-2006 05:31 PM

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.


All times are GMT -5. The time now is 03:12 PM.