how to declare a double dimensional array of pointers to its memberfunctions in the class?

Discussion in 'C++' started by ramu, Sep 13, 2011.

  1. ramu

    ramu Guest

    Hi,
    how to declare a double dimensional array of pointers to its
    member functions in the class?

    And how can we access it?

    I have done like this

    class A;

    typedef void (A::*fp)();

    class A{
    fp array[2][2];
    }


    and am trying ti call the function like below:

    A *aptr = new A;

    (aptr->*array[1][1])();

    Is this correct?

    Regards
     
    ramu, Sep 13, 2011
    #1
    1. Advertising

  2. ramu

    Werner Guest

    Re: how to declare a double dimensional array of pointers to itsmember functions in the class?

    On Sep 13, 9:36 am, ramu <> wrote:
    > Hi,
    >      how to declare a double dimensional array of pointers to its
    > member functions in the class?
    >
    > And how can we access it?
    >
    > I have done like this
    >
    > class A;
    >
    > typedef void (A::*fp)();
    >
    > class A{
    >                 fp array[2][2];
    >
    > }
    >
    > and am trying ti call the function like below:
    >
    > A *aptr = new A;
    >
    > (aptr->*array[1][1])();
    >
    > Is this correct?
    >
    > Regards


    Does it compile? ;-)

    class A;

    typedef void (A::*fp)();

    struct A
    {
    fp array[2][2];

    A()
    {
    array[0][0] = &A::f1;
    array[0][1] = &A::f2;
    array[1][0] = &A::f3;
    array[1][1] = &A::f4;

    }
    void f1(){ std::cout << "f1\n"; }
    void f2(){ std::cout << "f2\n"; }
    void f3(){ std::cout << "f3\n"; }
    void f4(){ std::cout << "f4\n"; }
    };

    int main()
    {
    A *aptr = new A;
    (aptr->*(aptr->array[1][1]))();
    }

    Regards,

    Werner
     
    Werner, Sep 13, 2011
    #2
    1. Advertising

  3. ramu

    Werner Guest

    Re: how to declare a double dimensional array of pointers to itsmember functions in the class?

    On Sep 13, 9:36 am, ramu <> wrote:
    > Hi,
    >      how to declare a double dimensional array of pointers to its
    > member functions in the class?
    >
    > And how can we access it?
    >
    > I have done like this
    >
    > class A;
    >
    > typedef void (A::*fp)();
    >
    > class A{
    >                 fp array[2][2];
    >
    > }
    >
    > and am trying ti call the function like below:
    >
    > A *aptr = new A;
    >
    > (aptr->*array[1][1])();
    >
    > Is this correct?
    >
    > Regards


    Another take on the same thing. Happy to have criticism, but
    I don't know what you would be using this for. I would use
    vectors instead of arrays. I would not allow public access
    to the table (at the very least).

    In addition to "the very least", you could perhaps return
    a proxy encapsulating the row of a table, and overload the
    subscript operator for A and for the proxy, as well as the
    function operator for the proxy. This would encapsulate
    it nicely and disallow the use of invalid member function
    pointers.


    #include <vector>
    #include <iostream>

    // I've moved the typedef to the class. I'm assuming you have
    // good reason not to, but I would not make the typedef global.

    struct A
    {

    A()
    : callbackTable_( 2, MemFuncSeq( 2 ) )
    {
    callbackTable_[0][0] = &A::f1;
    callbackTable_[0][1] = &A::f2;
    callbackTable_[1][0] = &A::f3;
    callbackTable_[1][1] = &A::f4;
    }
    void operator()( int row, int col )
    {
    (this->*callbackTable_.at( row ).at( col ))();
    }
    int rowCount() const{ return callbackTable_.size(); }
    int colCount() const{ return callbackTable_[0].size(); }

    private:
    typedef void (A::*PtrMemFunc)();
    typedef std::vector<PtrMemFunc> MemFuncSeq;
    std::vector<MemFuncSeq> callbackTable_;

    void f1(){ std::cout << "f1\n"; }
    void f2(){ std::cout << "f2\n"; }
    void f3(){ std::cout << "f3\n"; }
    void f4(){ std::cout << "f4\n"; }
    };

    int main()
    {
    A a;
    for( int row = 0; row < a.rowCount(); ++row )
    {
    for( int col = 0; col < a.colCount(); ++col )
    {
    a( row, col );
    }
    }
    }

    Kind regards,

    Werner
     
    Werner, Sep 13, 2011
    #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. Alf P. Steinbach
    Replies:
    0
    Views:
    438
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,929
    Default User
    Aug 19, 2003
  3. Venkat
    Replies:
    4
    Views:
    977
    Venkat
    Dec 5, 2003
  4. Sydex
    Replies:
    12
    Views:
    6,510
    Victor Bazarov
    Feb 17, 2005
  5. Michael Haupt
    Replies:
    2
    Views:
    415
Loading...

Share This Page