Life time of static pointer to member function

J

jon wayne

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
 
I

Ian

jon said:
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
 
D

Dave Rahardja

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
 
G

Greg

jon said:
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
 
D

Divick

To call a member function through a member function pointer does
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.
 
D

Divick

To call a member function through a member function pointer does
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
 
G

Greg

Divick said:
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
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top