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

Discussion in 'C++' started by James.D, Mar 4, 2004.

  1. James.D

    James.D Guest

    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
    James.D, Mar 4, 2004
    #1
    1. Advertising

  2. James.D

    John Carson Guest

    "James.D" <> wrote in message
    news:c27h0t$d76$99.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)
    John Carson, Mar 4, 2004
    #2
    1. Advertising

  3. James.D

    John Carson Guest

    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)
    John Carson, Mar 4, 2004
    #3
  4. James.D

    Rolf Magnus Guest

    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.
    Rolf Magnus, Mar 4, 2004
    #4
  5. James.D

    David Harmon Guest

    On Thu, 4 Mar 2004 23:12:26 +0800 in comp.lang.c++, "James.D"
    <> 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.
    David Harmon, Mar 4, 2004
    #5
  6. James.D

    James.D Guest

    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" <> дÈëÓʼþ
    news:...
    > On Thu, 4 Mar 2004 23:12:26 +0800 in comp.lang.c++, "James.D"
    > <> 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.
    >
    James.D, Mar 5, 2004
    #6
  7. James.D

    James.D Guest

    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" <> дÈëÓʼþ
    news:...
    > 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)
    >
    James.D, Mar 5, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fraser Ross
    Replies:
    4
    Views:
    1,028
    Fraser Ross
    Aug 14, 2004
  2. Replies:
    12
    Views:
    710
    fluden
    Feb 11, 2005
  3. Jahagirdar Vijayvithal S
    Replies:
    2
    Views:
    427
    Jahagirdar Vijayvithal S
    Aug 7, 2005
  4. Praetorian
    Replies:
    11
    Views:
    2,337
    James Kanze
    Apr 3, 2008
  5. somenath
    Replies:
    2
    Views:
    149
    somenath
    Aug 29, 2013
Loading...

Share This Page