Archived

This topic is now archived and is closed to further replies.

pointer to member function?

This topic is 5523 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

lo, I know I can create a pointer to a normal function but, does anyone know if there''s any way to create a pointer to a member function? Cheers, Case

Share this post


Link to post
Share on other sites
Here is one solution.


  
class XYZ
{
public:
...
void DisplayObject(const unsigned nPos);
};

typedef void (XYZ::*DObjPtr)(const unsigned nPos);

DObjPtr pFunction = &XYZ::DislayObject;


Kuphryn

Share this post


Link to post
Share on other sites
don''t forget - member functions are members of an object. so you have to associate an object with the function pointer before you can call it.

the only exception is static member functions which don''t use any member data so don''t need an object (and don''t get a ''this'' pointer passed to them)

the function-pointer link should do you fine.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Cheers ppl. I''d figured out the first bit of declaring the pointer, but I hadn''t figured out to associate the pointer with an instance using something like:

myInstance.*myPointer(...);

That link was very helpful, George2. Thanks. I''m sure that stuff wasn''t in my Deitel&Deitel book... oh well.

Case.

Share this post


Link to post
Share on other sites