繁体   English   中英

C ++指针函数到其他类函数

[英]C++ Pointer function to other class function

我需要帮助在C ++上传递一个函数指针。 我无法将类的一个函数链接到其他函数。 我会解释。 无论如何,我会把我的程序的代码恢复,它比这里暴露的代码大得多,但为了更容易我只把我需要的部分工作得很好。

我有一个类( MainSystem ),里面有一个指向另一个类( ComCamera )的对象指针。 最后一个类是SocketServer ,我希望当套接字接收到任何数据时,它会将连接函数发送给MainSystem。

ComCamera是一个与更多类共享的资源,我需要将函数ComCamera :: vRecvData与MainSystem :: vRecvData或其他类的其他函数关联,以便在接收数据时将de数据发送给函数类关联。

任何人都可以帮助我吗?

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

输出将是:

来自摄像机的MainSystem RECV数据(4):示例

你可以让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);});
}

尽管最初的问题还有太多的代码需要通过朋友。

在这里你想要的:

#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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM