dottedquad
dottedquad

Reputation: 1391

How do I call a class by passing it's object and member function to another function in c++?

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()
{

}

Upvotes: 0

Views: 178

Answers (2)

Healer
Healer

Reputation: 290

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;
}

Upvotes: 2

bahrami703i
bahrami703i

Reputation: 33

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)

Upvotes: 1

Related Questions