access 'typedef'ed member function

Discussion in 'C' started by pushpat, Jul 4, 2012.

  1. pushpat

    pushpat New Member

    Joined:
    Feb 14, 2012
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Hi folks,
    I am learning member function pointers, wrote following program to understand typedef of function (of other class eg: test) inside other class(eg: exam) and then its access.:smug:

    I have tried to use (e.*m)(8) but its gave
    err:‘m’ was not declared in this scope

    Could you please tell me how to call function 'add' using e.m
    I have added comments inline for reference


    Code:
    
    #include<iostream>
    using namespace std;
    
    class test
    {
    public:
    int add(int k){cout<<"test::add"<<endl;
    return k+1;
    }
    
    };
    
    class exam
    {
    public:
    typedef int (test::*ptr)(int);
    ptr m;
    };
    
    int main()
    {
    exam e;
    test t;
    e.m = &test::add;
    (e.m)(8); //Err:must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘e.exam::m (...)’, e.g. ‘(... ->* e.exam::m) (...)’
    
     /** followed (function_ptr)(arg) . what is the correct way to call add using e.m **/
    return 0;
    }
    
     
    Last edited: Jul 4, 2012
  2. debugEnthu

    debugEnthu New Member

    Joined:
    Jul 5, 2012
    Messages:
    11
    Likes Received:
    3
    Trophy Points:
    0
    U need to mention he class name of which the add() function is the member...

    Modify line
    (e.m)(8);
    To
    t .*(e.m) (8)
     
    shabbir likes this.
  3. debugEnthu

    debugEnthu New Member

    Joined:
    Jul 5, 2012
    Messages:
    11
    Likes Received:
    3
    Trophy Points:
    0
    Please consider this on


    U need to mention he class name of which the add() function is the member...

    Modify line
    (e.m)(8);
    To
    (t .*(e.m)) (8)
     
  4. pushpat

    pushpat New Member

    Joined:
    Feb 14, 2012
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    thank you debugEnthu, its working :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice