472,122 Members | 1,414 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Life time of static pointer to member function

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
7 2797
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
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
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
>>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
>>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
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
Oh, thanks for the correction.

Sep 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bryan Parkoff | last post: by
3 posts views Thread by JaeHyun, Roh | last post: by
5 posts views Thread by Michael Oswald | last post: by
5 posts views Thread by Tim Frink | last post: by
reply views Thread by leo001 | last post: by

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.