473,394 Members | 1,878 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,394 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 2990
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
4
by: Morgan Cheng | last post by:
I tried to build a CThread on linux which imitate the behavior of java Thread. I think this is still in the scope of C++. First, I tried this way #include <pthread.h> class CThread { public:...
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
3
by: JaeHyun, Roh | last post by:
I tried to store a static member function pointer to some function pointer... just like this.. class CSomeClass { public : static CSomeClass *Create() { return NULL; };
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
1
by: Assertor | last post by:
Hi All, Except function pointer to a static member funciton of a class (or a function). As the following code, I could pointer a non-static member function of a class. And "Equal" was...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
5
by: Michael Oswald | last post by:
Hello all, I'm working on a project where I came across some situations, where the GUI library works with normal C callbacks. The code has been implemented by many people, so I came across...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.