What is the correct grammar to make a function call by using static member data which is a pointer t

Discussion in 'C++' started by zaeminkr@gmail.com, Jul 6, 2007.

  1. Guest

    I have class member data which is a pointer to class member function.

    However, I'm fail to compile the code.

    What is the correct grammar to make a function call by using static
    member data which is a pointer to a member function?


    class Test
    {
    public :
    typedef int (Test::*pfnOperate)(int x, int y);

    static const pfnOperate m_arpOperate[2];

    public :
    int Add(int x, int y);
    int Subtract(int x, int y);
    void Foo();
    };

    const Test::pfnOperate Test::m_arpOperate[2] = {Test::Add,
    Test::Subtract};

    int Test::Add(int x, int y)
    {
    return x + y;
    }

    int Test::Subtract(int x, int y)
    {
    return x - y;
    }

    void Test::Foo()
    {
    int result;

    result = (Test::m_arpOperate[1])(10, 5); // error C2064: term does
    not evaluate to a function
    }

    int main(int argc, char* argv[])
    {
    Test t;

    t.Foo();
    return 0;
    }
    , Jul 6, 2007
    #1
    1. Advertising

  2. Robert Bauck Hamar, Jul 6, 2007
    #2
    1. Advertising

  3. Lionel B Guest

    Re: What is the correct grammar to make a function call by usingstatic member data which is a pointer to a ordinary class member function?

    On Fri, 06 Jul 2007 05:00:27 -0700, zaeminkr wrote:

    > I have class member data which is a pointer to class member function.
    >
    > However, I'm fail to compile the code.
    >
    > What is the correct grammar to make a function call by using static
    > member data which is a pointer to a member function?
    >
    >
    > class Test
    > {
    > public :
    > typedef int (Test::*pfnOperate)(int x, int y);
    >
    > static const pfnOperate m_arpOperate[2];
    >
    > public :
    > int Add(int x, int y);
    > int Subtract(int x, int y);
    > void Foo();
    > };
    >
    > const Test::pfnOperate Test::m_arpOperate[2] = {Test::Add, Test::Subtract};


    g++ insists I write:

    const Test::pfnOperate Test::m_arpOperate[2] = {&Test::Add, &Test::Subtract};

    I'm not clear on whether the standard actually requires this.

    > int Test::Add(int x, int y)
    > {
    > return x + y;
    > }
    >
    > int Test::Subtract(int x, int y)
    > {
    > return x - y;
    > }
    >
    > void Test::Foo()
    > {
    > int result;
    >
    > result = (Test::m_arpOperate[1])(10, 5); // error C2064: term does not evaluate to a function



    result = (this->*m_arpOperate[1])(10, 5);

    > }
    >
    > int main(int argc, char* argv[])
    > {
    > Test t;
    >
    > t.Foo();
    > return 0;
    > }


    --
    Lionel B
    Lionel B, Jul 6, 2007
    #3
  4. Guest

    On 7 6 , 9 17 , Robert Bauck Hamar <> wrote:
    > wrote:
    > > I have class member data which is a pointer to class member function.

    >
    > > However, I'm fail to compile the code.

    >
    > > What is the correct grammar to make a function call by using static
    > > member data which is a pointer to a member function?

    >
    > http://www.parashift.com/c -faq-lite/pointers-to-members.html#faq-33.5
    >
    > --
    > rbh


    Thanks but I already found that and it does not work.
    , Jul 6, 2007
    #4
    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,023
    Fraser Ross
    Aug 14, 2004
  2. aling
    Replies:
    6
    Views:
    466
    Xiaobin.Huang
    Oct 30, 2005
  3. dolphin
    Replies:
    3
    Views:
    1,323
    Pete Becker
    Dec 5, 2007
  4. Stephen Howe
    Replies:
    2
    Views:
    276
    Stephen Howe
    Nov 6, 2012
  5. somenath
    Replies:
    2
    Views:
    142
    somenath
    Aug 29, 2013
Loading...

Share This Page