CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Thumbs up Self Member Function Pointer

    Hello to all, i have wrote a self member function pointer.

    I try to do callback to member function but the statement is not executed by the compiler. I try to debug and found nothing wrong.

    Code:
    #ifndef SELF_MEMBER_FUNCTION_PTR_H
    #define SELF_MEMBER_FUNCTION_PTR_H
    
    #include <iostream>
    
    class myClass
    {
    private:
     typedef void (myClass::*MemberFunctionPtr)(); 
     MemberFunctionPtr theMFPtr[4];
    
    public:
     myClass()
     {
      theMFPtr[0] = &myClass::function1;
      theMFPtr[1] = &myClass::function2;
      theMFPtr[2] = &myClass::function3;
      theMFPtr[3] = &myClass::function4;
    
     }
     
     
     void function1() {std::cout << "function1";}
     void function2() {std::cout << "function2";}; 
     void function3() {std::cout << "function3";}; 
     void function4() {std::cout << "function4";}; 
    
     void callBack(int choice)
     {
      this->theMFPtr[choice];
     }
    
    
    };
    
    
    #endif
    
    
    
    #include <iostream>
    
    
    #include "Class.h"
    
    using namespace std;
    
    
    int main()
    {
     myClass theObject;
    
     theObject.callBack(1);
    
     return 0;
    }
    this->theMFPtr[choice]; does not get executed by the compiler.

    Please help.

    Thanks.
    Thanks for your help.

  2. #2
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Self Member Function Pointer

    use correct operator, which in this case is ->*
    Code:
      (this->*theMFPtr[choice])();

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Self Member Function Pointer

    Thanks.

    How about using std::map ?

    My code:

    Code:
    public:
    	typedef std::map<int,  void (HumanResource::*)() > MemberFuncPtr;
    private:
    StaffInfo theStaffInfo;
    MemberFuncPtr theMemberFuncPtr;
    
    HumanResource::HumanResource()
     : Human(), theStaffInfo(StaffInfo())
    {
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(1, &HumanResource::Add));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(2, &HumanResource::Delete));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(3, &HumanResource::Modified));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(4, &HumanResource::Search));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(5, &HumanResource::Register));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(6, &HumanResource::TimeIn));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(7, &HumanResource::TimeOut));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(8, &HumanResource::Logout));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(9, &HumanResource::Exit));
    
    }
    
    theMemberFuncPtr.find(choice)->second;
    I need two solution for the value of map.
    1. Using member function pointer
    2. Using functor.

    I refer to http://www.codeguru.com/forum/showth...hreadid=480274

    Lindley mentioned that since member function taking a implicit this pointer and yet the member function pointer taking void parameter. It's mismatch for the function pointer.


    Question:
    1. How to bind a member function as functor using boost::bind() and other adaptor function ?
    2. What is the solution for value of map using member function pointer ?
    Last edited by Peter_APIIT; July 6th, 2009 at 02:57 AM.
    Thanks for your help.

  4. #4
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Self Member Function Pointer

    Quote Originally Posted by Peter_APIIT View Post
    Thanks.

    How about using std::map ?
    That could be a better choice for your other problem (RAII Idiom).
    I need two solution for the value of map.
    1. Using member function pointer
    2. Using functor.
    I don't see the need to use std::map in a way you're describng.
    So neither. but if you want, use functor.
    I refer to http://www.codeguru.com/forum/showth...hreadid=480274

    Lindley mentioned that since member function taking a implicit this pointer and yet the member function pointer taking void parameter. It's mismatch for the function pointer.
    And Lindely is right.
    Question:
    1. How to bind a member function as functor using boost::bind() and other adaptor function ?
    2. What is the solution for value of map using member function pointer ?
    A functor is an object whose call operator() is overloaded.
    Code:
    #include <iostream>
    
    class myClass
    {
    public:
        myClass(){}
    
        void callBack(int choice) const
        {
            switch(choice)
            {
                case 1:
                    function1();
                    break;
                case 2:
                    function2();
                    break;
            }
        }
    
        bool operator()(const std::string& name) const
        {
            function3(name);
            return name.empty();
        }
        void operator()(int choice) const
        {
            callBack(choice);
        }
    
    private:
        void function1() const {std::cout << "function1";}
        void function2() const {std::cout << "function2";}
        void function3(const std::string& name)const
        {
            std::cout << "Hello " << name << std::endl;
        }
    };
    
    int main()
    {
        myClass theObject;
        theObject.callBack(2);
    
        theObject(2);          // equivalent as theObject.callBack(2);
        theObject("John Doe"); // calls function3
    
        return 0;
    }
    As you can see, a functor is more flexible than a pointer to member function,
    and works great with standard algorithms.

  5. #5
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Self Member Function Pointer

    Hello to potatocode, you code is good but this is not i looking for. I bag your pardon because i didn't explain it clearly. I now how to do it using operator() but i just want to code it different way.


    I need to declare a map with int as key , and functor as value. Problem, i don't know how to declare one.

    Then i would like insert it to map in constructor.
    Code:
    typedef std::map<int,  HumanResource() > MemberFuncPtr;
    
    MemberFuncPtr theMemberFuncPtr;
    
    HumanResource::HumanResource(): Human(), theStaffInfo(StaffInfo())
    {
      theMemberFuncPtr.insert(MemberFuncPtr::value_type(1, std::mem_func_ref(&HumanResource::Add)));
      // Using bind or mem_func_ref()
    I hope i explain it clearly.

    Thanks.
    Thanks for your help.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Self Member Function Pointer

    HumanResource is a class, is it not? Thus, the typedef should be:
    Code:
    typedef std::map<int,  HumanResource> MemberFuncPtr;
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Self Member Function Pointer

    laserlight, thanks for your help. You are brilliant.

    Code:
    theMemberFuncPtr.insert(MemberFuncPtr::value_type(1, boost::bind(&HumanResource::Add, *this)));


    error C2665: 'std:air<_Ty1,_Ty2>:air' : none of the 3 overloads could convert all the argument types:
    How to solve the incompatible type here ?
    Last edited by Peter_APIIT; July 7th, 2009 at 02:10 AM.
    Thanks for your help.

  8. #8
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Self Member Function Pointer

    Peter, sometimes I get you clearly but often times I don't quite get you xD

    you already defined a function table in myClass,
    what's the reason creating another one??

    binders usually returns a functor and adapters generally encapsulates a method by
    turning a function into a function object.
    meaning, the binders and adapters work better when you work with multiple functions
    and these functions get to pass as parameters to one another.

    You can't use std::bind1st or std::bind2nd with a function with more than 2 parameters.
    And you must know the parameter types of the class that you want to call member function directly.

    You need to also keep in mind that these standard functions can be expensive.
    My example using switch does the job as your fuction table, but it's much simpler and less expensive.
    So there are many things you need to consider when you use these binders.

    However,
    to use the returned function object from bind1st,
    the object must be of a type inherited from binary_function,
    and your HumanResource, which is not a subclass of it, can't be stored as a (K, V) value in a map.
    In this case, you can use the binder1st to match the return type from bind1st.
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    #include <functional>
    
    using namespace std;
    
    class myClass
    {
    public:
        myClass(){}
    
        void function1(myClass*) {std::cout << "function1\n";};
        void function2(myClass*) {std::cout << "function2\n";};
        void function3(myClass*) {std::cout << "function3\n";};
        void function4(myClass*) {std::cout << "function4\n";};
    };
    
    int main()
    {
        typedef binder1st<mem_fun1_t<void, myClass, myClass*> > CType;
        typedef map<string, CType>::value_type VType;
    
        map<string, CType> m;
    
        myClass theObject;
    
        m.insert(VType("up", bind1st(mem_fun(&myClass::function1), &theObject)));
        m.insert(VType("down", bind1st(mem_fun(&myClass::function2), &theObject)));
        m.insert(VType("left", bind1st(mem_fun(&myClass::function3), &theObject)));
        m.insert(VType("right", bind1st(mem_fun(&myClass::function4), &theObject)));
    
        string cmd;
        cout << "Enter command: ";
        cin >> cmd;
    
        map<string, CType>::const_iterator iter = m.find(cmd);
    
        if(iter != m.end())
        {
            (iter->second)(&theObject);
        }
        else
        {
            cout << "Invalid Command" << endl;
        }
    
        return 0;
    }
    But as you can see, I used a dummy pointer to match the function signature.

    Bottome line is that I don't think bind1st and mem_fun suit your design need at all.
    (i.e, if I understood your design in the first place :P)
    Last edited by potatoCode; July 7th, 2009 at 04:42 PM.

  9. #9
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Self Member Function Pointer

    I bag your pardon because i didn't explain my problem clearly. Perhaps code tell everything.

    Code:
    class HumanResource : public Human
    {
    public:
    	typedef std::map<int, HumanResource > MemberFuncPtr;
    
    private:
    		StaffInfo theStaffInfo;
    		MemberFuncPtr theMemberFuncPtr;
    	
    public:
    
    	HumanResource();
    	HumanResource(const std::string&, const std::string&);
    	~HumanResource();
    
    
    
    
    };
    
    
    HumanResource::HumanResource()
     : Human(), theStaffInfo(StaffInfo())
    {
    	// boost::bind(pointer-to-member-function, object to invoke on, args) 
    /*	theMemberFuncPtr.insert(MemberFuncPtr::value_type(1, boost::bind(&HumanResource::Add, _1)(*this)) );
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(2, boost::bind(&HumanResource::Delete, 0)) );
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(3, &HumanResource::Modified));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(4, &HumanResource::Search));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(5, &HumanResource::Register));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(6, &HumanResource::TimeIn));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(7, &HumanResource::TimeOut));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(8, &HumanResource::Logout));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(9, &HumanResource::Exit));
    */
    }
    I would like to insert functor to the map from convert my member function to functor using bind but it generate incompatible type between the declaration and my insert statement.

    Code:
    typedef std::map<int, HumanResource > MemberFuncPtr;
    MemberFuncPtr theMemberFuncPtr;
    
    theMemberFuncPtr.insert(MemberFuncPtr::value_type(1, boost::bind(&HumanResource::Add, _1)(*this)) );
    Thanks.
    Thanks for your help.

  10. #10
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Self Member Function Pointer

    Quote Originally Posted by Peter_APIIT View Post
    I bag your pardon because i didn't explain my problem clearly. Perhaps code tell everything.
    Nah, it could be who's not getting it.
    It's the same thing but how about
    Code:
    #include <iostream>
    #include <map>
    #include <functional>
    
    using namespace std;
    
    class StaffInfo;
    class Human {};
    
    class HumanResource : public Human
    {
    public:
    	//typedef std::map<int, HumanResource*> MemberFuncPtr;
        typedef binder1st<mem_fun1_t<void, HumanResource, HumanResource*> > bindType;
        typedef map<int, bindType> MemberFuncPtr;
    
    	void Add(HumanResource*) { cout << "Add" << endl; }
    	// rest of the methods...
    
    	void testCall()
    	{
            MemberFuncPtr::const_iterator iter = theMemberFuncPtr.begin();
    	    (iter->second)(this);
    	}
    
    private:
    		StaffInfo* theStaffInfo;
    		MemberFuncPtr theMemberFuncPtr;
    public:
    
    	HumanResource();
    	HumanResource(const std::string&, const std::string&){};
    	~HumanResource(){};
    };
    
    HumanResource::HumanResource()
    {
    	// no need for boost::bind
    	// boost::bind(pointer-to-member-function, object to invoke on, args)
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(1,
            bind1st(mem_fun(&HumanResource::Add), this)));
    
    	/*
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(2, &HumanResource::Delete, 0)) );
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(3, &HumanResource::Modified));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(4, &HumanResource::Search));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(5, &HumanResource::Register));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(6, &HumanResource::TimeIn));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(7, &HumanResource::TimeOut));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(8, &HumanResource::Logout));
    	theMemberFuncPtr.insert(MemberFuncPtr::value_type(9, &HumanResource::Exit));
    	*/
    }
    
    int main()
    {
        HumanResource res;
        res.testCall();
    
    }
    If this is not what you're looking for, I'm sorry I couldn't help you any further...
    I admire your efforts nontheless.

  11. #11
    Join Date
    Aug 2008
    Posts
    18

    Re: Self Member Function Pointer

    #pragma warning(disable:4786)

    #include <iostream>
    #include <map>
    using namespace std;

    class myClass
    {
    private:

    typedef map<int, void (myClass::*)(), less<int> > MyFunctionPointerMap;

    MyFunctionPointerMap::iterator theIterator;
    MyFunctionPointerMap mfp;


    public:
    myClass()
    {

    mfp.insert(MyFunctionPointerMap::value_type(0,&myClass::function1));
    mfp.insert(MyFunctionPointerMap::value_type(1,&myClass::function2));
    mfp.insert(MyFunctionPointerMap::value_type(2,&myClass::function3));
    mfp.insert(MyFunctionPointerMap::value_type(3,&myClass::function4));

    }


    void function1() {std::cout << "function1";}
    void function2() {std::cout << "function2";};
    void function3() {std::cout << "function3";};
    void function4() {std::cout << "function4";};

    void callBack(int choice)
    {

    (this->*mfp[1])();

    }


    };

    int main()
    {
    myClass theObject;

    theObject.callBack(1);

    return 0;
    }

    Above Code will Solve problem i think..

  12. #12
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Self Member Function Pointer

    If this is not what you're looking for, I'm sorry I couldn't help you any further...
    I admire your efforts nontheless.
    Yes, this is i want. Don't feel frustration.

    By the way, mem_func_t construct a functor and return to bind1st to the further processing.

    I not fully understand this code does.

    Question:
    1. What mem_func_t return to bind1st ?
    2. How bind1st working ?

    Code:
    typedef binder1st< mem_fun1_t<void, HumanResource, HumanResource*> > bindType;
        
    theMemberFuncPtr.insert(MemberFuncPtr::value_type(1,
            bind1st(mem_fun(&HumanResource::Add), this)));
    I also do a little research but can't fully understand your code.

    Thanks.
    Thanks for your help.

  13. #13
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Self Member Function Pointer

    I hope someone can explain that to us.
    Thanks for your help.

  14. #14
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Self Member Function Pointer

    Quote Originally Posted by Peter_APIIT View Post
    I hope someone can explain that to us.
    Read my post again (#8)

    Following the instructions given here on the forum is equally important as learning how to use debugger.
    These help that many members contribute are not any different from the explanation found on 1,600 Java APIs,
    nor do they lack any technicalities found on MSDN, or any reference materials found on the net.
    People who don't see the help given to them in this way ask the same thing over and over again.
    You've been here long enough to know better.

    I have a suggestion.
    For any 3 classes that you have finished,
    what do you think about writing what the classs is about(overview), list all the member functions and their usage,
    the number of parameters and the return type, const or non-const, and of course, with soem code samples?

    And you should know by now that srishi1302's example is the correct one to use for your needs, not mine.
    Last edited by potatoCode; July 11th, 2009 at 04:48 PM.

  15. #15
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Self Member Function Pointer

    Actually, my design is create a function table for all member function using map with key is int and value is functor. I know how to create array of function pointer. Therefore, i prefer another approaches.

    Hope you can understand my design.

    I have read your post 8 and 10 again but still cannot understand it.

    Can you explain in example related to the code in post 10 ?

    Question:
    1. What mem_func_t return to bind1st ?
    2. How bind1st working ?

    Thanks.
    Thanks for your help.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured