Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Pointer to C++ function (OT)

Author: Dan Newman

Date: 11:50:01 03/23/01

Go up one level in this thread


On March 23, 2001 at 10:01:54, Peter Fendrich wrote:

>I probably should know this - kind of embarrassing....
>
>In C I sometimes used this kind of construction:
>  int intParm; char strParm[100];
>  void (* FP)(char *, int *);
>  .
>  .
>  .
>  FP = (void ( *)(char *, int *)) &Function; // cast to function pointer
>  (*FP)(strParm, &intParm);                  // call the function
>
>
>In C++ I can't manage to do this.
>Anyone knows how to use a pointer to a function in a class in C++?
>
>//Peter

Here's an example of using pointers to member functions.

class Foo {
  public:
    void func1( char *, int *);
    void func2( char *, int *);
};


int main()
{
    void (Foo::*fp)(char *, int *);

    fp = &Foo::func1;

    Foo fooobj;
    int d;

    (fooobj.*fp)( "hello", &d);

    return 0;
}

Kind of ugly, isn't it?  I always forget how to do it and have to look it up.
Notice you need an object for the hidden "this" pointer for non-static
member functions.  I'm not sure of the syntax for static member functions.
Hope this helps...

-Dan.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.