473,386 Members | 2,042 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,386 software developers and data experts.

Function Pointer to a member function

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
Jul 22 '05 #1
4 1971

"Chris Bruyere" <bruyere_AT@_telus.net> wrote in message:

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


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

Jul 22 '05 #2
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
Jul 22 '05 #3

"Chris Bruyere" <bruyere_AT@_telus.net> wrote in message
news:pan.2004.02.13.04.42.56.783150@_telus.net...
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?


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
Jul 22 '05 #4

"Chris Bruyere" <bruyere_AT@_telus.net> wrote in message
news:pan.2004.02.13.04.42.56.783150@_telus.net...
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

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
7
by: jon wayne | last post by:
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...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...

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.