LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-05-2006, 03:59 PM   #1
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Rep: Reputation: 15
Angry 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
 
Old 01-05-2006, 04:04 PM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 01-05-2006, 04:04 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.

Last edited by dmail; 01-05-2006 at 04:05 PM.
 
Old 01-05-2006, 04:39 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 01-05-2006, 04:50 PM   #5
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
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
 
Old 01-05-2006, 05:13 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 01-05-2006, 05:50 PM   #7
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
Hello graeme,
How else would you do it?

Alaa
 
Old 01-05-2006, 06:14 PM   #8
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
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
 
Old 01-05-2006, 06:54 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 01-06-2006, 05:31 PM   #10
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto Assign Multiple Static Public IP Addresses under SBC's PPPoE Static Ip system o trekgraham Linux - Networking 8 04-17-2007 10:51 AM
shared libraries, dlopen and static member variables? (C/C++) Thinking Programming 2 12-19-2005 12:55 AM
Is there any static ARP for static IP network? linux_lover2005 Linux - Networking 2 05-18-2005 12:01 PM
LQ member progress status from member to senior member............. emailssent LQ Suggestions & Feedback 3 10-11-2004 01:31 PM
FC2 Overriding static if in favor of dhcp system set for static pkraus109 Linux - Networking 8 09-21-2004 11:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration