C++课间交流2种方式
Communicating 2 way between class in C++
我试图实现的基本思想是从类B调用类A的方法,反之亦然。这是我的代码:
主.cpp
void AppCB (uint8_t num, TempB* inst ){
printf("Application Level Callback Called!!!n");
}
void main(int, char*[]) {
printf("Starting Up!!!n");
TempB instB( 1, funcptr_arg1 (&AppCB) );
instB.funcB();
}
回调.cpp
void TempA :: tempa_cb (void){
printf("Calling Callback to Bn");
tempBobj->funcB_cb();
}
void TempA :: funcA (){
// some delay
tempa_cb();
}
void TempA :: registerTempB (TempB *tempbobj){
this->tempBobj = tempbobj;
}
TempB :: TempB ( uint8_t num , funcptr_arg1 AppCallback){
printf("Constructor TempBn");
slot = num;
funtptrB = AppCallback;
instA.registerTempB(this);
}
void TempB :: funcB (){
instA.funcA();
}
void TempB :: funcB_cb (){
printf ("Call from TempAn");
this->funtptrB(event, this); // <<<<<<<<<<<< this pointer is corrupted here. Hence it is not able to call the AppCB function in main.cpp file.
}
回调.h
typedef void (*funcptr_arg1)(int, void *);
class TempB;
class TempA {
private:
uint8_t tempa;
TempB *tempBobj;
public:
void funcA();
void tempa_cb(void);
void registerTempB (TempB *tempbobj);
};
class TempB {
private:
uint8_t slot;
funcptr_arg1 funtptrB;
TempA instA;
public:
TempB (uint8_t num, funcptr_arg1 funtptrB);
void funcB();
// this function should be called from object of A.
void funcB_cb();
};
从上面的代码中,我想知道:
- 为什么"this"指针(用于调用函数的指针)会像代码中标记的那样损坏?
- 有没有更好的方法来实现这一目标?
干杯维内特。
下面是两个类的交互实例的C++示例。我还没有编译和测试代码,只是在我的浏览器中编写的,但它的描述性足以给你这个想法:
哑巴:
// This forward declaration will allow us to use B pointers
// in the declaration of class A, but it will not allow to
// access its members. For this reason the methods of A that
// want to access the members of B must be defined after the
// declaration of class B.
class B;
class A
{
public:
A(B* b): m_B(b) {}
void CalledByB() {}
void PerformWork();
private:
B* m_B;
};
class B
{
public:
void SetA(A* a) { m_A = a; }
void CalledByA() {}
void PerformWork();
private:
A* m_A;
};
虚拟.cpp:
#include "dummy.h"
// Since we already included the declaration of class B by
// including dummy.h we can start using the members of B
// through the m_B pointer. By defining this method as an
// inline in the declaration of class A we couldn't have
// called m_B->CalledByA() because at that point the declaration
// of class B was unavailable.
void A::PerformWork()
{
m_B->CalledByA();
}
// In case of class B we could have put this code to the header
// too because we declared class B after class A so at the time
// of the declaration of B the full declaration of A was available
// for the compiler.
void B::PerformWork()
{
m_A->CalledByB();
}
int main()
{
// Initialization
B b;
A a(&b);
b.SetA(&a);
// Work
a.PerformWork();
b.PerformWork();
}
相关文章:
- 在C++中将函数压缩为两种方式
- 哪种方式更快?究竟发生了什么,我们没有看到什么?
- 将函数作为参数传递的两种方式之间的区别
- 初始化结构的两种方式
- !=的另一种方式
- 以一种方式使用字符串选择(如果语句)
- 这两种方式有什么区别吗?
- 在 OpenGL 中渲染对象的哪种方式更有效
- 在哪种方式搜索容器中的元素的方式更有效
- 一种方式链接列表和不需要的第一个元素AFRTER填充
- 由于您可以在C 中以两种方式声明复制构造函数,这是正确的方式
- 重载输入/输出运算符,为什么它以这种方式工作而不是以另一种方式工作
- 哪种方式将 HHOOK 消息返回到系统更好
- 哪种方式更快
- 哪种方式更好地获得64位整数中较低的32位
- 是 2s 补码存储负数的一种方式
- 尝试在Qt中连接隐藏信号的两种方式之间做出决定
- C++课间交流2种方式
- 哪种方式更适合定义类型特征或行为
- 哪种方式更适合阵列访问