0

How do I execute a member's function by passing the object and the member's function to another function in c++. I do understand the answer to my question is out there; however, I do not know what this is called. So far I created 2 files, exeFunc.h and exeFunc.cpp. Their code consist of:

exeFunc.h

/*
File: exeFunc.h

Header file for exeFunc Library.
*/

#ifndef EXEFUNC_H
#define EXEFUNC_H

#include "mbed.h"
#include "msExtensions.h"
#include "cfExtensions.h"

#include <map>

class exeFunc
{

public:
    exeFunc(msExtensions &msExt, cfExtensions &cfExt);

private:

    void _splitFuncFromCmd();
    void _attachCallback();

    msExtensions &_msExt;
    cfExtensions &_cfExt;

    //FunctionPointer _p;
};

#endif

exeFunc.cpp

/*
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)
{
    //_cfExt.checkConfigForFirstStart();
    //_p.attach(&_cfExt, &cfExtensions::checkConfigForFirstStart);

    //_p.call();

}

void exeFunc::_splitFuncFromCmd()
{

}

void exeFunc::_attachCallback()
{

}
dottedquad
  • 1,371
  • 6
  • 24
  • 55
  • 2
    Is this what you are looking for: http://stackoverflow.com/questions/130322/how-do-you-pass-a-member-function-pointer – jogojapan Nov 28 '12 at 05:44
  • Do you want to pass the object, or a function pointer pointing to the member function? – SingerOfTheFall Nov 28 '12 at 05:46
  • @SingerOfTheFall So far I have passed all my objects by Reference, but I have never created a function pointer pointing to the member function. Basically I am attempting to kill two birds with one stone by passing both the object and the member function so I can execute the function in a general fashion. – dottedquad Nov 28 '12 at 05:54
  • @SingerOfTheFall Yes, I could do it that way. Is there a way that I can do that dynamically by passing the object and method to another function? – dottedquad Nov 28 '12 at 05:57
  • @jogojapan That is what I am looking for; however, the template and all the other arguments in the class methods are clouding my understanding. – dottedquad Nov 28 '12 at 06:35

2 Answers2

2

I wrote a completed example, may helps

class MyClass
{
public:
    MyClass(int b)
        :_b(b)
    {
    }

    int Foo(int a)
    {
        return a * _b;
    }
    int _b;
};

typedef int (MyClass::*MFP)(int);

int get_result(MyClass* obj, MFP mfp)
{
    int r = (obj->*mfp)(5); // 30
    return r;
}

int _tmain(int argc, _TCHAR* argv[])
{
    MFP mfp = &MyClass::Foo;

    MyClass m(6);
    get_result(&m, mfp);


    return 0;
}
Healer
  • 290
  • 1
  • 7
1

You call it by another function.if you have an independent function.
To be honesty your question is not completely clear.However :

int F(int,int,int);
int g();
//main scope 
F(g(),a,b)
  • The best way that I can explain what I am attempting to do is, create a basic generic function pointer by passing the object and method to execute that function in a general way. – dottedquad Nov 28 '12 at 05:56
  • 1
    you should define a pointer to a function . what do you mean dynamically? – bahrami703i Nov 28 '12 at 06:09
  • By dynamically I mean pass the pointers to another function to execute the member's function. 'dynamically' was probably the wrong choice of word. – dottedquad Nov 28 '12 at 06:31