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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James.D

    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


  • John Carson

    #2
    Re: A pointer in a class points to an other member function in the same class?

    "James.D" <ddddh@vip.sina .com> wrote in message
    news:c27h0t$d76 $1@mail.cn99.co m[color=blue]
    > Hi, I met such a problem:
    >
    > //---------------------
    > // .h
    > class CA
    > {
    > protected:
    > void (CA::*)m_pfn();
    >[/color]

    Wrong syntax. It should be:

    void (CA::*m_pfn)();
    [color=blue]
    > 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
    > }[/color]

    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)

    Comment

    • John Carson

      #3
      Re: A pointer in a class points to an other member function in the same class?

      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)

      Comment

      • Rolf Magnus

        #4
        Re: A pointer in a class points to an other member function in the same class?

        James.D wrote:
        [color=blue]
        > 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.[/color]

        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.

        Comment

        • David Harmon

          #5
          Re: A pointer in a class points to an other member function in the same class?

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

          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.

          Comment

          • James.D

            #6
            Re: A pointer in a class points to an other member function in the same class?

            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" <source@netcom. com> дÈëÓʼþ
            news:408d550e.2 60629703@news.w est.earthlink.n et...[color=blue]
            > On Thu, 4 Mar 2004 23:12:26 +0800 in comp.lang.c++, "James.D"
            > <ddddh@vip.sina .com> was alleged to have written:[color=green]
            > > (pObj->*m_pfn)(); // here I got two compile errors[/color]
            >[color=green]
            > >I compiled my project in VS.net 2003, and in the line I marked, I got 2
            > >errors: error C2597 and C2568[/color]
            >
            > 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.
            >[/color]


            Comment

            • James.D

              #7
              Re: A pointer in a class points to an other member function in the same class?

              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" <donaldquixote@ datafast.net.au > дÈëÓʼþ
              news:40475461@u senet.per.parad ox.net.au...[color=blue]
              > I see that I have just wasted my time since you posted this question in[/color]
              two[color=blue]
              > 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[/color]
              newsgroup[color=blue]
              > etiquette.
              >
              >
              > --
              > John Carson
              > 1. To reply to email address, remove donald
              > 2. Don't reply to email address (post here instead)
              >[/color]


              Comment

              Working...