简体   繁体   English

如何通过将类的对象和成员函数传递给C ++中的另一个函数来调用类?

[英]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++. 如何通过将对象和成员函数传递给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. 到目前为止,我创建了2个文件exeFunc.h和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 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()
{

}

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

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将成员函数传递给另一个对象的成员函数C ++ - Passing member function to another object's member function C++ 如何像正常的C函数一样使用正确的“ this”指针调用C ++类成员函数? (指向类成员函数的指针) - How do I call a C++ Class member function with the correct 'this' pointer like a normal C function? (pointer to class member function) How do I call a member function inside a non-member function in C++ with an object created outside the non-member function? - How do I call a member function inside a non-member function in C++ with an object created outside the non-member function? 如何从另一个函数调用属于类成员的函数? - How do I call a function that is member of a class, from another function? C ++将模板类的成员函数传递给另一个函数 - C++ Passing a member function of a template class to another function 如何在成员函数中的C ++中创建优雅的for_each(),而操作函数是同一类中的另一个成员函数? - How do I create an elegant for_each() in C++ inside a member function where the operating function is another member function in the same class? C ++将类成员传递给另一个类函数 - C++ passing a class member into another class function 如何调用另一个班级的成员function? - how to call another class's member function? 我如何获得一个类成员函数来访问另一个类成员函数的私有成员? - How do i get a class member function to have access to another class member function's private member? 如何从C ++调用matlab成员函数? - How do I call matlab member function from C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM