Question on arrays of pointers to functions in C++

Discussion in 'C++' started by C. J. Clegg, Apr 10, 2006.

  1. C. J. Clegg

    C. J. Clegg Guest

    Consider the following code...

    class MyClass
    {
    public:
    int Fct1( void );
    int Fct2( void );
    int Fct3( void );

    void ExecFcts( void );
    };

    int MyClass::fct1( void )
    {
    // do some stuff
    return 0
    }

    int MyClass::fct2( void )
    {
    // do some different stuff
    return 0
    }

    int MyClass::fct3( void )
    {
    // do some still different stuff
    return 0
    }

    void MyClass::ExecFcts( void )
    {
    struct
    {
    int anIntVar;
    int (MyClass::*TheFct)( void );
    } arrayOfFcts[ ] =
    {
    { 0, &MyClass::Fct1 },
    { 1, &MyClass::Fct2 },
    { 2, &MyClass::Fct3 }
    };

    // This all compiles OK up to here.
    // Then...
    //
    // try to call one of the functions in the struct array

    arrayOfFcts[ 0 ].TheFct( );
    // This makes a compile error
    }

    int main( int argc, char** argv )
    {
    MyClass theClass;

    theClass.ExecFcts();

    return 0;
    }

    I have read through the FAQ and every post I can find on dejanews on
    this topic, in fact the FAQ educated me on the difference between a
    pointer to a member function and a pointer to a non-member function
    and the need to prepend "MyClass::" to the function names in the
    struct array, and allowed me to get a clean compile up to the point
    indicated in the code comment above.

    But, I can't get the correct syntax for the function call.

    I have tried every combination I can think of of:

    arrayOfFcts[ 0 ].MyClass::TheFct( );
    arrayOfFcts[ 0 ].*MyClass::TheFct( );
    arrayOfFcts[ 0 ].MyClass::*TheFct( );

    ( ... with and without parens sprinkled liberally about...)

    ....and no luck.

    I have done my research ... honest ... and I'm out of ideas.
    C. J. Clegg, Apr 10, 2006
    #1
    1. Advertising

  2. C. J. Clegg wrote:
    > [...]
    > // try to call one of the functions in the struct array
    >
    > arrayOfFcts[ 0 ].TheFct( );


    Should be

    (this->*(arrayOfFcts[0].TheFct))();

    > // This makes a compile error
    > }
    > [...]


    And next time please don't type your code directly into the posting,
    you made several typos.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 10, 2006
    #2
    1. Advertising

  3. C. J. Clegg <> wrote:
    > Consider the following code...
    >
    > class MyClass
    > {
    > public:
    > int Fct1( void );
    > int Fct2( void );
    > int Fct3( void );
    >
    > void ExecFcts( void );
    > };
    >
    > int MyClass::fct1( void )
    > {
    > // do some stuff
    > return 0
    > }
    >
    > int MyClass::fct2( void )
    > {
    > // do some different stuff
    > return 0
    > }
    >
    > int MyClass::fct3( void )
    > {
    > // do some still different stuff
    > return 0
    > }
    >
    > void MyClass::ExecFcts( void )
    > {
    > struct
    > {
    > int anIntVar;
    > int (MyClass::*TheFct)( void );
    > } arrayOfFcts[ ] =
    > {
    > { 0, &MyClass::Fct1 },
    > { 1, &MyClass::Fct2 },
    > { 2, &MyClass::Fct3 }
    > };
    >
    > // This all compiles OK up to here.
    > // Then...
    > //
    > // try to call one of the functions in the struct array
    >
    > arrayOfFcts[ 0 ].TheFct( );
    > // This makes a compile error
    > }
    >
    > int main( int argc, char** argv )
    > {
    > MyClass theClass;
    >
    > theClass.ExecFcts();
    >
    > return 0;
    > }
    >
    > I have read through the FAQ and every post I can find on dejanews on
    > this topic, in fact the FAQ educated me on the difference between a
    > pointer to a member function and a pointer to a non-member function
    > and the need to prepend "MyClass::" to the function names in the
    > struct array, and allowed me to get a clean compile up to the point
    > indicated in the code comment above.
    >
    > But, I can't get the correct syntax for the function call.
    >
    > I have tried every combination I can think of of:
    >
    > arrayOfFcts[ 0 ].MyClass::TheFct( );
    > arrayOfFcts[ 0 ].*MyClass::TheFct( );
    > arrayOfFcts[ 0 ].MyClass::*TheFct( );


    Almost. I suppose you know that member functions need an object to
    work with, considering you did the research :) In your code, you do not
    provide any object, though. This is why this won't work. Try this
    instead:

    (this->*arrayOfFcts[0].TheFct) ();

    For a 'regular' call (at the same code location!, ie. inside a
    member function), as in 'Fct1 ();', you do not need to explicitly
    provide an object, because 'this' is used implicitly. When you use
    pointers, you must provide the object explicitly .. even inside a member
    function.

    hth
    --
    jb

    (reply address in rot13, unscramble first)
    Jakob Bieling, Apr 10, 2006
    #3
  4. C. J. Clegg

    C. J. Clegg Guest

    On Mon, 10 Apr 2006 11:55:38 -0400, "Victor Bazarov"
    <> wrote:

    >Should be
    >
    > (this->*(arrayOfFcts[0].TheFct))();


    Good afternoon, Victor and Jakob.

    Thanks to both of you for that... it's the one variation I didn't try
    (naturally), and it worked fine.

    >And next time please don't type your code directly into the posting,


    Hmmm ... Really? OK, I can refrain from doing that in the future, but
    how do I provide code samples that illustrate the problem?

    Thanks again...
    C. J. Clegg, Apr 10, 2006
    #4
  5. C. J. Clegg <> wrote:

    > how do I provide code samples that illustrate the problem?


    You should compile the code until the only errors (if any) left are
    the ones you are having problems with.

    regards
    --
    jb

    (reply address in rot13, unscramble first)
    Jakob Bieling, Apr 10, 2006
    #5
  6. C. J. Clegg wrote:
    > On Mon, 10 Apr 2006 11:55:38 -0400, "Victor Bazarov"
    > <> wrote:
    > [..]
    >> And next time please don't type your code directly into the posting,

    >
    > Hmmm ... Really? OK, I can refrain from doing that in the future, but
    > how do I provide code samples that illustrate the problem?


    Copy and paste. I am betting your text editor and news reader allow you
    to do so.
    Victor Bazarov, Apr 10, 2006
    #6
  7. C. J. Clegg

    C. J. Clegg Guest

    On Mon, 10 Apr 2006 18:48:20 +0200, "Jakob Bieling"
    <> wrote:

    >C. J. Clegg <> wrote:
    >
    >> how do I provide code samples that illustrate the problem?

    >
    > You should compile the code until the only errors (if any) left are
    >the ones you are having problems with.


    Ah, OK, understood.

    I actually did that, sort of ... the code I put in my posting (I guess
    nobody calls these postings "articles" anymore) was a vastly
    oversimplified version of some code that is in an application I'm
    writing, and I figured you guys wouldn't want to wade through all the
    baggage that comes with the real thing, so I simplified it to the
    simplest possible form and used that. I did compile the actual code
    but didn't compile my simplified sample first ... sorry.
    C. J. Clegg, Apr 11, 2006
    #7
  8. C. J. Clegg

    C. J. Clegg Guest

    On Mon, 10 Apr 2006 13:50:57 -0400, "Victor Bazarov"
    <> wrote:

    >Copy and paste. I am betting your text editor and news reader allow you
    >to do so.


    Ah, OK, I misunderstood what you said, thought you meant not to put
    the code in the posting at all.

    Got it. :)
    C. J. Clegg, Apr 11, 2006
    #8
    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. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    384
  2. Replies:
    1
    Views:
    645
    -berlin.de
    Mar 28, 2005
  3. Philipp
    Replies:
    21
    Views:
    1,126
    Philipp
    Jan 20, 2009
  4. Francesco
    Replies:
    2
    Views:
    1,106
    Francesco
    Nov 6, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    672
Loading...

Share This Page