473,668 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3022
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**********@g mail.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
4597
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 variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
4
2375
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: void start(); void run();
1
3972
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 bound inside struct or class to become global functions. It makes easier for me to use "struct.memberfunction()" instead of "globalfunction()" when I have to use dot between struct and member function rather than global function. I do not have...
3
1314
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
4401
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 same invocation from a DistantlyRelated class. What actually happened was that the compiler produced: error C2247: 'A::function' not accessible because 'CloselyRelated' uses 'private' to inherit from 'A' I'm guessing that the above compiler...
1
2552
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 printed, the pointer has meaning. But how can I use it??
6
7668
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 a .NET application and thus am looking into writing a managed C++ wrapper for in vs2005. Furthermore, this library has many callback hooks which need to be implemented by the C++ wrapper. These callback functions are declared as "extern C...
5
2498
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 different versions of callbacks (see the code below for 3 variants). Variant1: a normal static member function is used to redirect the callback to a member function Variant2: a static member function is declared, but at the definition the
5
4652
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
8893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8656
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6209
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5681
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4205
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4380
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2791
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1786
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.