C++ Pointer function to other class function

Viewed 145

I need help with passing a function pointer on C++. I can't linkage one function for a class to other function. I will explain. Anyway I will put a code resume of my program, it is much larger than the code expose here but for more easier I put only the part I need to it works fine.

I have one class (MainSystem) and inside I have an object pointer to the other class (ComCamera). The last class is a SocketServer, and I want when the socket received any data, it sends to the linkage function to MainSystem.

ComCamera is a resource Shared with more class and I need to associate the functions ComCamera::vRecvData to a MainSystem::vRecvData or other function of other class for the call when receive data and send de data to the function class associate.

Can Anyone help to me?

EDDITED - SOLUTION BELOW

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

Output will be:

MainSystem RECV Data From Camera(4): Example

2 Answers

You can have ComCamera::vRecvData be of type std::function<void(int, std::string)> and then have ComCamera::vLinkRecvFunction() be like this:

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

and have MainSystem constructor be like this:

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

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

Still though the original question has way too much code to go through friend.

Here what you want :

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

Output will be :

0

6

I still don't get why would you do something like this. But maybe this is what you wanted as per your comments. To know more read this wonderful PDF.

Related