Initializing an array of pointers to member functions insideconstructor & invoking them

Discussion in 'C++' started by Ramesh, Jul 30, 2008.

  1. Ramesh

    Ramesh Guest

    Hi,

    I am trying to create an array of pointers to member functions inside
    my class. When I created a global array of type pfn & initialized with
    member functions and copy it back to the member function pointer array
    (Handlers) - compiler doesnt let me do that.
    I got an error as below:

    "invalid use of non-static member function bool test::func1(long int)"

    I am looking for two things at this point

    1) how to initialize the member array of pointers (I prefer to do that
    in the constructor)

    2) call them from a member function funcw by their index?

    Here is my code:

    using namespace std;
    #include <iostream>
    #define MAXFN 2

    class test;

    typedef bool (*pfn) (long);

    class test {

    private:

    long var;
    pfn Handlers[MAXFN];

    public:

    test();
    bool func1(long);
    bool func2(long);
    bool funcw(long);
    void reg();
    };


    test::test() {
    }

    bool test::func1(long _p1) {
    var = _p1;
    }

    bool test::func2(long _p2) {

    cout << var << endl;
    cout << "New Value :" << var << endl;

    }

    bool test::funcw(long _i) {

    }

    void test::reg() {
    }

    main() {

    test t1;
    t1.func1(10);
    t1.func2(20);
    t1.funcw(1);

    }

    Thanks
    /R
    Ramesh, Jul 30, 2008
    #1
    1. Advertising

  2. Ramesh

    mlimber Guest

    mlimber, Jul 30, 2008
    #2
    1. Advertising

  3. Ramesh

    Ramesh Guest

    On Jul 30, 1:01 pm, mlimber <> wrote:
    > On Jul 30, 2:10 pm, Ramesh <> wrote:
    >
    > > I am trying to create an array of pointers to member functions inside
    > > my class. [snip]

    >
    > Victor gave you the proper synatx, and further explanation can be
    > found in the FAQs:
    >
    > http://www.parashift.com/c -faq-lite/pointers-to-members.html
    >
    > Cheers! --M


    Thanks Victor!

    I read the FAQ though, feel I should invoke member functions through
    another member function rather than a macro - just to make debugging
    easier.

    Regards
    /R
    Ramesh, Jul 30, 2008
    #3
    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. Hamish
    Replies:
    3
    Views:
    553
    Alf P. Steinbach
    Jan 25, 2008
  2. Hicham Mouline
    Replies:
    0
    Views:
    420
    Hicham Mouline
    Apr 23, 2009
  3. Hicham Mouline
    Replies:
    1
    Views:
    404
    Michael DOUBEZ
    Apr 24, 2009
  4. paul
    Replies:
    8
    Views:
    699
    Alf P. Steinbach
    Apr 30, 2009
  5. K. Frank
    Replies:
    20
    Views:
    378
Loading...

Share This Page