Thread: How do I pass an instantiated object and it's member function to another function?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Casa Grande, AZ
    Posts
    4

    How do I pass an instantiated object and it's member function to another function?

    Hi all. I have been stuck on this idea for days now. From my understanding, to call a member function by pointer, I will need two things: A pointer to the class and a pointer to the function. With that said, my two files are exeFunc.h and exeFunc.cpp Their code consist of:

    exeFunc.h
    Code:
    /*
    File: exeFunc.h
    
    
    Header file for exeFunc Library.
    */
    
    
    #ifndef EXEFUNC_H
    #define EXEFUNC_H
    
    
    #include "mbed.h"
    #include "msExtensions.h"
    #include "cfExtensions.h"
    #include "GenericFunctionHandler.h"
    #include <map>
    #include <utility>
    
    
    class exeFunc
    {
    
    
    public:
        exeFunc(msExtensions &msExt, cfExtensions &cfExt);
    
    
        void _exeCallback();
    
    
    
    
    private:
        void _splitFuncFromCmd();
        void _attachCallback();
    
    
        msExtensions &_msExt;
        cfExtensions &_cfExt;
    
    
    };
    
    
    
    #endif
    exeFunc.cpp
    Code:
    /*
    File: exeFunc.cpp
    
    
    Execute functions in other Sensor libraries/classes
    
    
    Constructor
    */
    
    
    #include "mbed.h"
    #include "ConfigFile.h"
    #include "msExtensions.h"
    #include "cfExtensions.h"
    #include "exeFunc.h"
    
    
    #include <map>
    #include <string>
    
    
    using namespace std;
    
    
    exeFunc::exeFunc(msExtensions &msExt, cfExtensions &cfExt) : 
        _msExt(msExt), _cfExt(cfExt)
    {
    
    
    }
    
    
    void exeFunc::_splitFuncFromCmd()
    {
    
    
    }
    
    
    void exeFunc::_exeCallback()
    {
        
        
    }
    I already have my instantiated objects _msExt(msExt) and _cfExt(cfExt). I would like to use void exeFunc::_exeCallback() function as the function to finally execute the function from the object and the member function that were typed into the arguments of exeCallback(). For example, if I type _exeCallback(&_cfExt, &cfExtensions::checkConfigForFirstStart) that should execute the member function. That is the part that I am having a hard time with. What should I do to achieve this without using bind?

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Maybe using a member function templates could help you, for instance:
    Code:
    #include <iostream>
    using namespace std;
    
    struct exeFunc{
    
        template<class T, class F>
        void exeCallback(T* t, F f){
            (t->*(f))();
        }
    
    };
    
    
    struct cfExtensions{
        void f(){
            cout << "cfExtensions::f()\n";
        }
    
        void g(){
            cout << "cfExtensions::g()\n";
        }
    };
    
    struct msExtensions{
        void f(){
            cout << "msExtensions::f()\n";
        }
    
        void g(){
            cout << "msExtensions::g()\n";
        }
    
        void h(){
            cout << "msExtensions::h()\n";
        }
    };
    
    int main(){
        cfExtensions cfe;
        msExtensions mse;
    
        exeFunc ef;
        ef.exeCallback(&cfe, &cfExtensions::f);
        ef.exeCallback(&cfe, &cfExtensions::g);
        ef.exeCallback(&mse, &msExtensions::f);
        ef.exeCallback(&mse, &msExtensions::g);
        ef.exeCallback(&mse, &msExtensions::h);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: cannot call member function without object
    By TIMBERings in forum C++ Programming
    Replies: 7
    Last Post: 04-15-2010, 04:21 AM
  2. Pass object to function
    By swgh in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2008, 07:57 AM
  3. how to pass an ifstream object to a function
    By chintugavali in forum C++ Programming
    Replies: 14
    Last Post: 12-18-2007, 09:58 PM
  4. How to pass member functions into a function object...
    By TeenWolf in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 01:01 PM
  5. syntax to pass a member function pointer to another class?
    By reanimated in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2003, 05:24 PM