473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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

Oct 29 '05 #1
6 1441
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
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
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

"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
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
why do this?
use non-static member function instead.

Oct 30 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dmitry.freitor | last post by:
Why would someone call a non-static provate member function from another non-static member function of the same class via the this pointer? Thanks. DF
1
by: zx.zhangxiong | last post by:
Dear all, I'm puzzled about the usage of class member function. Any help would be appreciated. class Account { ...
7
by: Ook | last post by:
What am I doing wrong? This code gives a compile error: 'SortedList<T>::insert' : illegal call of non-static member function. I've tried several variations of this, but keep getting the same error....
10
by: shanknbake | last post by:
I'm getting the following compile-time error: error C2352: 'Person::getCount' : illegal call of non-static member function Here is my getCount function declaration:...
2
by: mahesh | last post by:
Can anyone direct me to the place where i find the solution for the error message "cannot call member function 'X' without object"??? thanks in advance
20
by: Bruce. | last post by:
I am trying to call a member function through a pointer to it and can't get the syntax exactly right. This compiles with an error C2064, term does not evaluate to a function taking 1 argument.. ...
3
by: zaeminkr | last post by:
I have class member data which is a pointer to class member function. However, I'm fail to compile the code. What is the correct grammar to make a function call by using static member data...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
12
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.