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

Life time of static pointer to member function

P: n/a
Hi
I'm a little confused here about the lifetime of a static pointer to
member function,

Say,
I declare,define & initialize a static ptr to mem function in the
header file of a class(the class is a helper Singleton class, and is
created & deleted as and when required) - Where does the static pointer
point to when every single instance of the class is deleted.
I presume it'll be dangling pointer- as the code seg to which it's
pointing to is no longer valid.

Looking forward to some insight on the same.

regards
JON

Sep 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Ian
jon wayne wrote:
Hi
I'm a little confused here about the lifetime of a static pointer to
member function,

Say,
I declare,define & initialize a static ptr to mem function in the
header file of a class(the class is a helper Singleton class, and is
created & deleted as and when required) - Where does the static pointer
point to when every single instance of the class is deleted.


If you are creating and deleting it, it isn't a singleton.

What do you initialise this pointer to? A code snipped would help.

Ian
Sep 21 '05 #2

P: n/a
On 20 Sep 2005 19:42:00 -0700, "jon wayne" <jo**********@gmail.com> wrote:
Hi
I'm a little confused here about the lifetime of a static pointer to
member function,

Say,
I declare,define & initialize a static ptr to mem function in the
header file of a class(the class is a helper Singleton class, and is
created & deleted as and when required) - Where does the static pointer
point to when every single instance of the class is deleted.
I presume it'll be dangling pointer- as the code seg to which it's
pointing to is no longer valid.

Looking forward to some insight on the same.


I think you're not using the term "Singleton" correctly. Nevertheless...

A static pointer to member function (once initialized correctly) points to the
address of a function. Creating and deleting objects does not change the
pointer--it will always point to that function.

Remember that member functions do not get "created" or "deleted" along with
objects. There is always only one instance of each member function, and its
location is fixed at link time.

-dr
Sep 21 '05 #3

P: n/a
jon wayne wrote:
Hi
I'm a little confused here about the lifetime of a static pointer to
member function,

Say,
I declare,define & initialize a static ptr to mem function in the
header file of a class(the class is a helper Singleton class, and is
created & deleted as and when required) - Where does the static pointer
point to when every single instance of the class is deleted.
I presume it'll be dangling pointer- as the code seg to which it's
pointing to is no longer valid.

Looking forward to some insight on the same.

regards
JON


The address of a member function, like the address of a regular
function, remains valid throughout the life of the program. So in this
case, the member function pointer is still valid - no matter whether
any objects of the member function's class exist and is valid even if
no objects of that class had ever been allocated at all.

To call a member function through a member function pointer does
require an instance of the class whose member function is being called
(the "this" object). So if you have deleted all objects of a class,
there will be no object around with which you could actually use the
member function pointer to call the member function. You would have to
create such an object if you wanted to use the member function pointer
to call the member function it points to.

Greg

Sep 21 '05 #4

P: n/a
>>To call a member function through a member function pointer does
require an instance of the class whose member function is being called
(the "this" object).


You don't need a class instance if the method is static. It could be
JON might be having a pointer to a static function altogether.

Sep 21 '05 #5

P: n/a
>>To call a member function through a member function pointer does
require an instance of the class whose member function is being called
(the "this" object).


You don't need a class instance if the method is static. It could be
JON might be having a pointer to a static function altogether.

Divick

Sep 21 '05 #6

P: n/a
Divick wrote:
To call a member function through a member function pointer does
require an instance of the class whose member function is being called
(the "this" object).


You don't need a class instance if the method is static. It could be
JON might be having a pointer to a static function altogether.


A member function pointer cannot point to a static member function. It
can point only to a non-static member function.

A function pointer, on the other hand, can point to a static member
function (as well as to a global function).

Perhaps some C++ source code will clarify how member function pointers
can be used:

class A
{
public:
static int sf() { return 5;}

int mf() { return -11; }
};

int main()
{
int (*funcPtr)(); // function pointer
int (A::*memFPtr)(); // member function pointer for A

funcPtr = &A::sf; // OK
memFPtr = &A::mf; // OK
funcPtr = &A::mf; // Error - A::mf is not static
memFPtr = &A::sf; // Error - A::sf is static

A a;

(*funcPtr)(); // OK calls A::sf
(a.*memFPtr)(); // OK calls a->sf();
(a.*funcPtr)(); // Error - requires a member function pointer
(*memFPtr)(); // Error - requires class A object
}

Sep 21 '05 #7

P: n/a
Oh, thanks for the correction.

Sep 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.