C++ 指向其他 class 函数的指针函数
C++ Pointer function to other class function
我需要有关在 C++ 上传递函数指针的帮助。我无法将 class 的一个函数链接到其他函数。我会解释。不管怎样,我会放一份我的程序的代码简历,它比这里公开的代码大得多,但为了更容易,我只放了我需要的部分,它工作正常。
我有一个 class (MainSystem
),在里面我有一个指向另一个 class (ComCamera
) 的对象指针。最后一个 class 是一个 SocketServer
,我希望当套接字接收到任何数据时,它发送到 MainSystem 的链接函数。
ComCamera 是与更多 class 共享的资源,我需要将函数 ComCamera::vRecvData 关联到 MainSystem::vRecvData 或其他 class 的其他函数,以便在调用时接收数据并将数据发送到函数 class associate.
有人可以帮我吗?
已编辑 - 解决方案如下
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 Data From Camera(4): Example
这是你想要的:
#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.
您可以让 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);});
}
尽管原始问题的代码太多,无法通过朋友。
我需要有关在 C++ 上传递函数指针的帮助。我无法将 class 的一个函数链接到其他函数。我会解释。不管怎样,我会放一份我的程序的代码简历,它比这里公开的代码大得多,但为了更容易,我只放了我需要的部分,它工作正常。
我有一个 class (MainSystem
),在里面我有一个指向另一个 class (ComCamera
) 的对象指针。最后一个 class 是一个 SocketServer
,我希望当套接字接收到任何数据时,它发送到 MainSystem 的链接函数。
ComCamera 是与更多 class 共享的资源,我需要将函数 ComCamera::vRecvData 关联到 MainSystem::vRecvData 或其他 class 的其他函数,以便在调用时接收数据并将数据发送到函数 class associate.
有人可以帮我吗?
已编辑 - 解决方案如下
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 Data From Camera(4): Example
这是你想要的:
#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.
您可以让 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);});
}
尽管原始问题的代码太多,无法通过朋友。