473,395 Members | 1,422 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,395 software developers and data experts.

A pointer in a class points to an other member function in the same class?

Hi, I met such a problem:

//---------------------
// .h
class CA
{
protected:
void (CA::*)m_pfn();

public:
CA();
void foo();

static void proc(CA *pObj); // NOTES it is a static member
};

//---------------------
// .cpp
CA::CA()
{
m_pfn = foo;
}

void CA::foo()
{
return;
}

void CA::proc(CA *pObj)
{
if (pObj->m_pfn)
(pObj->*m_pfn)(); // here I got two compile errors
}
I compiled my project in VS.net 2003, and in the line I marked, I got 2
errors: error C2597 and C2568
I don't know how to resolve this problem.

Any suggestions will be appreciated.

Thanks!
James
Jul 22 '05 #1
6 1588
"James.D" <dd***@vip.sina.com> wrote in message
news:c2**********@mail.cn99.com
Hi, I met such a problem:

//---------------------
// .h
class CA
{
protected:
void (CA::*)m_pfn();

Wrong syntax. It should be:

void (CA::*m_pfn)();
public:
CA();
void foo();

static void proc(CA *pObj); // NOTES it is a static member
};

//---------------------
// .cpp
CA::CA()
{
m_pfn = foo;
}

void CA::foo()
{
return;
}

void CA::proc(CA *pObj)
{
if (pObj->m_pfn)
(pObj->*m_pfn)(); // here I got two compile errors
}


Also wrong syntax. It should be

(pObj->*pObj->m_pfn)();

This syntax performs two functions.

1. The first thing you need to do is retrieve the function pointer. The
pObj->m_pfn on the RIGHT returns the pointer value. Call it function_ptr for
short, so we end up with

(pObj->*function_ptr)();

2. The second thing to do is to actually call the member function using the
function pointer. The remainder of the expression does just that. As with
all (non-static) member functions, you need to call the function by
binding it to an object.

To summarise, the object that pObj points to is needed twice: once as the
object storing the function pointer value, and once as the object that you
use to call the function (in this second capacity the object supplies a
"this" pointer so that the member function can access other members if
needed).
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2
I see that I have just wasted my time since you posted this question in two
other newsgroups and received an answer there. It is precisely for this
reason that multi-posting such as this is regarded as a breach of newsgroup
etiquette.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #3
James.D wrote:
Hi, I met such a problem:

//---------------------
// .h
class CA
{
protected:
void (CA::*)m_pfn();

public:
CA();
void foo();

static void proc(CA *pObj); // NOTES it is a static member
};

//---------------------
// .cpp
CA::CA()
{
m_pfn = foo;
}

void CA::foo()
{
return;
}

void CA::proc(CA *pObj)
{
if (pObj->m_pfn)
(pObj->*m_pfn)(); // here I got two compile errors
}
I compiled my project in VS.net 2003, and in the line I marked, I got
2 errors: error C2597 and C2568
I don't know how to resolve this problem.


And I don't know what error "C2597" and "C2568" are. Could you please
add the text of those messages? Not everyone has a Microsoft compiler
installed.

Jul 22 '05 #4
On Thu, 4 Mar 2004 23:12:26 +0800 in comp.lang.c++, "James.D"
<dd***@vip.sina.com> was alleged to have written:
(pObj->*m_pfn)(); // here I got two compile errors I compiled my project in VS.net 2003, and in the line I marked, I got 2
errors: error C2597 and C2568


There should be some explanatory text associated with the error reports.
Read the text and consider what it says. It's near impossible to
determine the nature of a compile error without reading the message.

Jul 22 '05 #5
I am so sorry about that.

Because the comiler I used is a Chinese version, so the text associated with
the error number is in Chinese.
I can't translate it into English prefectly.

:(
James
"David Harmon" <so****@netcom.com> дÈëÓʼþ
news:40****************@news.west.earthlink.net...
On Thu, 4 Mar 2004 23:12:26 +0800 in comp.lang.c++, "James.D"
<dd***@vip.sina.com> was alleged to have written:
(pObj->*m_pfn)(); // here I got two compile errors

I compiled my project in VS.net 2003, and in the line I marked, I got 2
errors: error C2597 and C2568


There should be some explanatory text associated with the error reports.
Read the text and consider what it says. It's near impossible to
determine the nature of a compile error without reading the message.

Jul 22 '05 #6
Sorry, I it is almost the first time I asking in newsgroup.

I really don't know the etiqette.
And I will notice this in the next time.

Thank you very much for your answer and your advice.
James
"John Carson" <do***********@datafast.net.au> дÈëÓʼþ
news:40******@usenet.per.paradox.net.au...
I see that I have just wasted my time since you posted this question in two other newsgroups and received an answer there. It is precisely for this
reason that multi-posting such as this is regarded as a breach of newsgroup etiquette.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #7

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,...
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: | last post by:
Say we have the following code defining TMyMsgHandler and TMyClass typedef void (*TOnMsgReceive) (TMyMessage Msg); class TMyMsgHandler { public: TMyMsgHandler(); virtual ~TMyMsgHandler();...
7
by: andylcx | last post by:
Hi all, I have a question about the code below. Any idea abuot that, thanks in advance! myclass.h class myclass { public: myclass(); ~myclass(); void setdata();
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...
10
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
12
by: WaterWalk | last post by:
Hello. I am rather confused by the type of a pointer to class data member. Many c++ texts say that pointer to data member has a special syntax. For example the following class: class MyClass {...
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 {
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.