C ++指向其他类函数的指针函数

时间:2018-08-27 07:02:51

标签: c++ class function-pointers

我需要在C ++上传递函数指针的帮助。我无法将一个类的一个函数链接到其他函数。我会解释。无论如何,我都会放置程序的代码履历,它比此处公开的代码大得多,但为了更容易,我只放置了我需要的部分即可正常工作。

我有一个类(MainSystem),内部有一个指向另一类的对象指针(ComCamera)。最后一个类是SocketServer,我想在套接字接收到任何数据时将其发送到MainSystem的链接函数。

ComCamera是与更多类共享的资源,我需要在接收数据并将数据发送到关联的函数类时将ComCamera :: vRecvData函数与MainSystem :: vRecvData或其他类的其他函数关联以进行调用。

有人可以帮我吗?

EDDITED-下方解决方案

main.cpp

#include <iostream>
#include <thread>  
#include <string>
#include <vector>
#include <cmath>
#include <string.h>
#include <stdio.h>
#include <exception>
#include <unistd.h>

using std::string;

class ComCamera {
public:
    std::function<void(int, std::string)> vRecvData;

    void vLinkRecvFunction(std::function<void(int, std::string)> vCallBack) {
        this->vRecvData = vCallBack;
    }

    void vCallFromCamera() {
        this->vRecvData(4, "Example");
    };
};

class MainSystem {
private:
    ComCamera *xComCamera;
public:
    MainSystem(ComCamera *xComCamera) {
        this->xComCamera = xComCamera;
        this->xComCamera->vLinkRecvFunction([this](int iChannelNumber, std::string sData) {vRecvData(iChannelNumber, sData); });
    }

    void vRecvData(int iNumber, string sData) {
        std::cout << "RECV Data From Camera(" + std::to_string(iNumber) + "): " << sData << std::endl;
    };
};

int main(void) {
    ComCamera xComCamera;
    MainSystem xMainSystem(&xComCamera);

    xComCamera.vCallFromCamera();

    return 0;
}

输出将是:

  

来自Camera(4)的MainSystem RECV数据:示例

2 个答案:

答案 0 :(得分:2)

您可以让ComCamera::vRecvData类型 std::function<void(int, std::string)>,然后让ComCamera::vLinkRecvFunction()像这样:

void ComCamera::vLinkRecvFunction(std::function<void(int, std::string)> callBack)
{
    this->vRecvData = callBack;
}

并让MainSystem 构造函数像这样:

MainSystem::MainSystem(ComCamera *xComCamera)
{
    using namespace std::placeholders;

    this->xComCamera = xComCamera;
    this->xComCamera->vLinkRecvFunction([this](int iNumber, std::string sData){vRecvData(number, sData);});
}

尽管原始问题仍然有太多代码无法通过朋友进行。

答案 1 :(得分:1)

这是您想要的:

#include<iostream>
using std::cout;

class A; //forward declare A
class B{
    public:
    void (A::*ptr)(int x); //Only declare the pointer because A is not yet defined.
};

class A{
    public:
    void increase_by(int x){
        a+=x;
    } // this function will be pointed by B's ptr
    int a = 0; // assume some data in a;
    B b; // creating B inside of A;
    void analyze(int y){ 
        (*this.*(b.ptr))(y);
    }   // Some function that analyzes the data of A or B; Here this just increments A::a through B's ptr           
};

int main(){
    A a; // creates A
    cout<<a.a<<"\n"; // shows initial value of a
    a.b.ptr = &A::increase_by; // defines the ptr that lies inside of b which inturns lies inside a
    a.analyze(3); // calls the initialize method
    (a.*(a.b.ptr))(3); // directly calls b.ptr to change a.a
    cout<<a.a; // shows the value after analyzing
    return 0;
}

输出将是:

  

0

     

6

我仍然不明白你为什么要这样做。但这也许就是您根据评论所想要的。 要了解更多信息,请阅读此精彩的PDF

相关问题