function pointer class member ?

Discussion in 'C++' started by sledge, Sep 16, 2010.

  1. sledge

    sledge Guest

    Hi,
    I am porting some legacy C code to C++. The code has a lot of member
    function pointers like

    class X {
    int (*a)(int,int);
    void (*b)(char t);
    int x;
    int f(int,int);
    };

    main()
    {
    // declare a pointer to member function
    void (X::*a) (int) = &X::f;
    This does not seem to work. What is the right syntax to handle the
    function pointer?

    Thanks
    Sledge


    }
     
    sledge, Sep 16, 2010
    #1
    1. Advertising

  2. sledge

    cpp4ever Guest

    On 09/16/2010 06:58 AM, sledge wrote:
    > Hi,
    > I am porting some legacy C code to C++. The code has a lot of member
    > function pointers like
    >
    > class X {
    > int (*a)(int,int);
    > void (*b)(char t);
    > int x;
    > int f(int,int);
    > };
    >
    > main()
    > {
    > // declare a pointer to member function


    void (X::*a)(int,int) = &X::f

    >
    > Thanks
    > Sledge
    >
    >
    > }


    The member function pointer was of the wrong type as you had the wrong
    parameter list. See the solution above

    HTH

    cpp4ever
     
    cpp4ever, Sep 16, 2010
    #2
    1. Advertising

  3. sledge

    Ian Collins Guest

    On 09/16/10 05:58 PM, sledge wrote:
    > Hi,
    > I am porting some legacy C code to C++. The code has a lot of member
    > function pointers like
    >
    > class X {
    > int (*a)(int,int);
    > void (*b)(char t);
    > int x;
    > int f(int,int);
    > };
    >
    > main()
    > {
    > // declare a pointer to member function
    > void (X::*a) (int) =&X::f;
    > This does not seem to work. What is the right syntax to handle the
    > function pointer?


    You appear to be confusing a pointer to a member function and a member
    that happens to be a function pointer!

    --
    Ian Collins
     
    Ian Collins, Sep 16, 2010
    #3
  4. sledge

    Goran Pusic Guest

    On Sep 16, 7:58 am, sledge <> wrote:
    > Hi,
    > I am porting some legacy C code to C++.


    If that is so, you should know first rule of porting, and that is:
    "Don't". Porting is risky, and C++ is awesome enough that you can wrap
    existing code (put a C++ façade on it) virtually without impacting
    performance. Did you try that first?

    > The code has a lot of member
    > function pointers like
    >
    > class X {
    >                int (*a)(int,int);
    >                void (*b)(char t);
    >                 int x;
    >                int f(int,int);
    >
    > };
    >
    > main()
    > {
    > // declare a pointer to member function
    >  void (X::*a) (int) = &X::f;
    > This does not seem to work. What is the right syntax  to handle the
    > function pointer?


    Obviously, this should be void (X::*a) (int, int) = &X::f; (note the
    second "int").

    On a more general note...

    Your C function pointers do not seem to belong to X (logically, not
    formally). If they "belonged", they would typically look like so:

    int (*a)(X* this, int param1, int param2); (they would have "explicit"
    this pointer.

    That said, function pointers like these are normally a sign of an
    attempt to get polymorphism in C (and if so, the above is not the best
    version). If that is the purpose of the code, then you should just
    switch to virtual functions.

    Goran.
     
    Goran Pusic, Sep 16, 2010
    #4
  5. On Sep 15, 10:58 pm, sledge <> wrote:
    > Hi,
    > I am porting some legacy C code to C++. The code has a lot of member
    > function pointers like
    >
    > class X {
    >                int (*a)(int,int);
    >                void (*b)(char t);
    >                 int x;
    >                int f(int,int);
    >
    > };
    >
    > main()
    > {
    > // declare a pointer to member function
    >  void (X::*a) (int) = &X::f;
    > This does not seem to work. What is the right syntax  to handle the
    > function pointer?


    This assignment is not possible (even after fixing the parameter list
    of X::*a from 'int' to 'int, int'). X::f is a non-static member
    function and X::a is a pointer to a free function. The two cannot be
    made compatible.

    - Kevin
     
    Kevin McCarty, Sep 16, 2010
    #5
  6. sledge

    Pavel Guest

    sledge wrote:
    > Hi,
    > I am porting some legacy C code to C++. The code has a lot of member
    > function pointers like
    >
    > class X {
    > int (*a)(int,int);
    > void (*b)(char t);
    > int x;
    > int f(int,int);
    > };
    >
    > main()
    > {
    > // declare a pointer to member function
    > void (X::*a) (int) =&X::f;
    > This does not seem to work. What is the right syntax to handle the
    > function pointer?
    >
    > Thanks
    > Sledge
    >
    >
    > }

    The following changed version of your example will compile although I am
    not sure whether it's what you wanted (other people who mentioned X::a
    class member does not seem to belong to the code were right: please note
    that it is not related to the local variable `a' defined in main(); the
    latter could be as well called `b' or anything without changing the
    class X definition):

    class X {
    int (*a)(int,int);
    void (*b)(char t);
    int x;
    public:
    int f(int,int) { return 0; }
    };

    int main() {
    int (X::*a) (int, int) = &X::f;
    }

    Hope this will help
    -Pavel
     
    Pavel, Sep 18, 2010
    #6
    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. Newsgroup - Ann
    Replies:
    5
    Views:
    657
    John Carson
    Jul 30, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,094
    Fraser Ross
    Aug 14, 2004
  3. Replies:
    12
    Views:
    737
    fluden
    Feb 11, 2005
  4. Praetorian
    Replies:
    11
    Views:
    2,383
    James Kanze
    Apr 3, 2008
  5. somenath
    Replies:
    2
    Views:
    178
    somenath
    Aug 29, 2013
Loading...

Share This Page