By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

How do you call a regular member function from a static member function?

P: n/a
How do you call a regular member function from a static member
function? Any idea?

Oct 29 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
By definition, you can't.
static member functions don't a particular instance of the class it
resides in.
usually, if you need an access to a specific instance, you must pass a
pointer to the instance to the static function.

Oct 29 '05 #2

P: n/a
TIT
aling sade:
How do you call a regular member function from a static member
function? Any idea?


class A {
public:
void f(){}
static void g() {
A a;
a.f();
}
};

int main() {
A::g();
return 0;
}

TIT
Oct 29 '05 #3

P: n/a
aling wrote:
How do you call a regular member function from a static member
function? Any idea?


Exactly the same way that you call a regular member function anywhere
else. There is nothing special about static member functions in this regard.

To call a regular member function you need an object of the appropriate
type. Then you use that to call the regular member function. E.g.

SomeObject obj;
obj.some_function();

The above code will work perfectly well in a static member function or
anywhere else.

john
Oct 29 '05 #4

P: n/a

"aling" <li*********@126.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
How do you call a regular member function from a static member
function? Any idea?


Is this a trick interview question or are you trying to write some code
dependent
on it, since this is likely to be a bad thing to do...post some code.

dave
Oct 29 '05 #5

P: n/a
Yes, this is a trick interview question .
Dave Townsend wrote:
Is this a trick interview question or are you trying to write some code
dependent
on it, since this is likely to be a bad thing to do...post some code.


Oct 30 '05 #6

P: n/a
why do this?
use non-static member function instead.

Oct 30 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.