Function Pointer to a member function

C

Chris Bruyere

Hi All, I just finished reading the FAQ page on fucntion points, for this
ng, and I just wanted to clarify something.

class MessageCenter
{
....

void addMessage(int i, Message* m); //implemented in .cpp file
}

class Person
{
public:

typef void (MessageCenter::*msgFunc)(int i, Message* m)

void setPostOfficePtr(msgFunc mf);
/*
in cpp file:
m_sendMsgToPostOffice = mf;
*/
....

private:

msgFunc m_sendMsgToPostOffice; //a fcn ptr, send a msg to his/her MsgCtr

}

class HumanResources
{
public:
HumanResources(..., MessageCenter* msgCtr);
/*
in cpp file:
m_msgCtr = msgCtr
*/
... newPerson(..);
/*
in cppfile:
Person* temp = new Person();
temp->setPostOfficePtr(MessageCenter::addMessage); <-----this is
what I would like to talk about
*/

....
private:
MessageCenter* m_msgCtr;
}

As it stands right now, it compiles fine, I haven't tested it yet, but it
should work. My question is this: will that function pointing to
MessageCetner::addMessage to the the MessageCenter pointer passed into
HumanResources? I'm curoius because I have not explictly told it to point
to that member function, so if i had multiple instances of MessageCenter
could that function pointer point to any one of them?

What I would really like is to have something like:

Person* temp = new ...
temp->setPostOfficePtr(m_msgCtr->addMessage)

-But this doesn't compile, when i do compile I get this message:

no matching function for call to `Person::setPostOfficePtr(<unknown type>)'
Person/Person.h:278: candidates are:
void Person::setPostOfficePtr(void (MessageCenter::*)(int, Message*))

So why does this happen? I am very curious to know why and of course, to
know how to fix it.

Thanks for you help. I'm sorry if this is confusing or unreadable, i tried
to comply with this ng's rules as best at I can.

Thanks again
Chris
 
J

Jonathan Turkanis

Chris Bruyere said:
As it stands right now, it compiles fine, I haven't tested it yet, but it
should work. My question is this: will that function pointing to
MessageCetner::addMessage to the the MessageCenter pointer passed into
HumanResources? I'm curoius because I have not explictly told it to point
to that member function, so if i had multiple instances of MessageCenter
could that function pointer point to any one of them?

First, the syntax for a pointer to member function would be
&MessageCetner::addMessage; you need the &, although some compilers
don't care.

Second, Member pointers do not refer to any particular class instance.
Given:
1. a member function pointer pf of type void
(MessageCenter::*)(int, Message*),
2. a MessageCenter instance mc,
3. an int i, and
4. a a Message* m,
you invoke the member pointer like so

(mc.*pf)(i, m)

If mc were a pointer, you would use

(mc->*pf)(i, m).


Unfortunately, C++ does not provide a special type for storing the
result of binding a member function pointer to a class instance. If
you want this functionality, you need to program it yourself, or use a
library like Loki or Boost.Function.

Jonathan
 
C

Chris Bruyere

are.
Second, Member pointers do not refer to any particular class instance.

So, if I had two seperate instances of the class, MessageCenter, I could
not be able to predict which class is being called?

Thanks
Chris
 
J

Jonathan Turkanis

Chris Bruyere said:
are. instance.

So, if I had two seperate instances of the class, MessageCenter, I could
not be able to predict which class is being called?

When you write

(mc.*pf)(i, m)

you call a member function of the instance mc. You can't incoke a
member function through a member function pointer without explicitly
providing an instance.

HTH

Jonathan
 
J

Jonathan Turkanis

Chris Bruyere said:
are. instance.

So, if I had two seperate instances of the class, MessageCenter, I could
not be able to predict which class is being called?

Thanks
Chris
 

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,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top