C++课间交流2种方式

Communicating 2 way between class in C++

本文关键字:2种 方式 C++      更新时间:2023-10-16

我试图实现的基本思想是从类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();
};

从上面的代码中,我想知道:

  1. 为什么"this"指针(用于调用函数的指针)会像代码中标记的那样损坏?
  2. 有没有更好的方法来实现这一目标?

干杯维内特。

下面是两个类的交互实例的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();
}