how to call method of the class which contains a pointer to other class method?

Discussion in 'C++' started by Pawel_Iks, Jul 30, 2007.

  1. Pawel_Iks

    Pawel_Iks Guest

    I have following class definition (A.h file):

    class A
    {
    public:
    int N;
    int count1(int n) {n++;}
    int count2(int n) {n+=5;}
    int otherFun(int n, int (*fun)(int));
    }

    and I want to implement otherFun method (in A.cpp file):

    int A::eek:therFun(int,int (*fun)(int))
    {
    int c=0,d;
    d=fun(c);
    }

    and call it (in main.cpp):

    int main()
    {
    A obj=A();
    int t;
    A.N=12;
    t=A.otherFun(N,test.count1);
    return 0;
    }

    and it doesn't work ... I have two questions:
    1) If there implementation of otherFun method is correct, and when
    it's correct how can i call this method in main() function?
    2) why it doesn't work? (for simply function - not class member - it
    works fine)
     
    Pawel_Iks, Jul 30, 2007
    #1
    1. Advertising

  2. Pawel_Iks wrote:
    > I have following class definition (A.h file):
    >
    > class A
    > {
    > public:
    > int N;
    > int count1(int n) {n++;}
    > int count2(int n) {n+=5;}
    > int otherFun(int n, int (*fun)(int));
    > }
    >
    > and I want to implement otherFun method (in A.cpp file):
    >
    > int A::eek:therFun(int,int (*fun)(int))
    > {
    > int c=0,d;
    > d=fun(c);
    > }
    >
    > and call it (in main.cpp):
    >
    > int main()
    > {
    > A obj=A();
    > int t;
    > A.N=12;
    > t=A.otherFun(N,test.count1);


    The usual beginner's mistake: a non-static member function is NOT
    compatible with a pointer to function. Look up and read about
    "pointer to member".

    > return 0;
    > }
    >
    > and it doesn't work ... I have two questions:
    > 1) If there implementation of otherFun method is correct, and when
    > it's correct how can i call this method in main() function?


    You can't.

    > 2) why it doesn't work? (for simply function - not class member - it
    > works fine)


    Sure. There is a difference between a function and a member function
    that is not static. You need to educate yourself on member funcitons
    and "pointers to members".

    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, Jul 30, 2007
    #2
    1. Advertising

  3. Pawel_Iks

    osmium Guest

    "Pawel_Iks" wrote:

    >I have following class definition (A.h file):
    >
    > class A
    > {
    > public:
    > int N;
    > int count1(int n) {n++;}
    > int count2(int n) {n+=5;}
    > int otherFun(int n, int (*fun)(int));
    > }
    >
    > and I want to implement otherFun method (in A.cpp file):
    >
    > int A::eek:therFun(int,int (*fun)(int))
    > {
    > int c=0,d;
    > d=fun(c);
    > }
    >
    > and call it (in main.cpp):
    >
    > int main()
    > {
    > A obj=A();
    > int t;
    > A.N=12;
    > t=A.otherFun(N,test.count1);
    > return 0;
    > }
    >
    > and it doesn't work ... I have two questions:
    > 1) If there implementation of otherFun method is correct, and when
    > it's correct how can i call this method in main() function?
    > 2) why it doesn't work? (for simply function - not class member - it
    > works fine)


    Try clicking on item 33 in this link.

    http://www.parashift.com/c -faq-lite/#table-of-contents
     
    osmium, Jul 30, 2007
    #3
  4. Pawel_Iks

    terminator Guest

    On Jul 30, 3:39 pm, Pawel_Iks <> wrote:
    > I have following class definition (A.h file):
    >
    > class A
    > {
    > public:
    > int N;
    > int count1(int n) {n++;}
    > int count2(int n) {n+=5;}
    > int otherFun(int n, int (*fun)(int));
    >
    > }
    >
    > and I want to implement otherFun method (in A.cpp file):
    >
    > int A::eek:therFun(int,int (*fun)(int))
    > {
    > int c=0,d;
    > d=fun(c);
    >
    > }
    >
    > and call it (in main.cpp):
    >
    > int main()
    > {
    > A obj=A();

    this works but it is better to simply write:
    A obj;
    > int t;
    > A.N=12;
    > t=A.otherFun(N,test.count1);


    this syntax belongs to C# not C++. instance member functions are
    different from static functions the 'A::eek:therFunc' takes a static
    function as its parameter.

    you can overload 'A::eek:therFunc' with this one:

    class A{
    public:
    int otherFunc (int,A& ,int(A::*)(int) );
    //the rest of class follows as before:
    ...
    };

    int A::eek:therFunc (int n,A& obj_ref,int(A::*method)(int) ){
    return (obj_ref.*method)(n);//call with object or referenc
    };

    int F(int){};
    static int G(int){};

    int main(){
    A a,test;
    a.otherFunc (1,test,&A::count1);//my version of otherFun
    a.otherFunc (2,F);//your version of otherFun
    a.otherFunc (3,G);//your version of otherFun
    return 0;
    };

    > 2) why it doesn't work? (for simply function - not class member - it
    > works fine)


    because you think that combination of a method with an object forms a
    static function but you are wrong.
    It is obvious that you are migrating from a newly introduced high-
    level language(java/C#) to the mixed-level C++.Things are different in
    different languages.

    regards,
    FM.
     
    terminator, Jul 31, 2007
    #4
    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.

Share This Page