C++ C+中类之间的双向通信+;

C++ C+中类之间的双向通信+;,c++,C++,我试图实现的基本思想是从类B调用类A的方法,反之亦然。 这是我的密码: main.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) );

我试图实现的基本思想是从类B调用类A的方法,反之亦然。 这是我的密码:

main.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();
}
void TempA :: tempa_cb (void){
    printf("Calling Callback to B\n");
    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 TempB\n");
    slot = num;
    funtptrB = AppCallback;
    instA.registerTempB(this);
}

void TempB :: funcB (){
    instA.funcA();
}

void TempB :: funcB_cb (){
    printf ("Call from TempA\n");
    this->funtptrB(event, this);  // <<<<<<<<<<<< this pointer is corrupted here. Hence it is not able to call the AppCB function in main.cpp file.
}
callback.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();
}
void TempA :: tempa_cb (void){
    printf("Calling Callback to B\n");
    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 TempB\n");
    slot = num;
    funtptrB = AppCallback;
    instA.registerTempB(this);
}

void TempB :: funcB (){
    instA.funcA();
}

void TempB :: funcB_cb (){
    printf ("Call from TempA\n");
    this->funtptrB(event, this);  // <<<<<<<<<<<< this pointer is corrupted here. Hence it is not able to call the AppCB function in main.cpp file.
}
从上面的代码中,我想知道:

  • 为什么“this”指针(用于调用函数的指针)会像代码中标记的那样损坏
  • 有没有更好的方法来实现这一点
  • 干杯,
    ViNET.

    这是一个C++实例,用于两个类的交互实例。我没有编译和测试代码,只是在我的浏览器中编写的,但它的描述性足以让您了解:

    虚拟。h:

    // 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;
    };
    
    dummy.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++程序中使用C函数指针,除非你必须和需要这个的C代码通信。这些类实例应该通过类指针相互通信。您会发现函数指针问题吗。这是我第一次听到这件事。谢谢你提供的信息。我是C++新手。因此,最好的选择是:-在C++程序中,尽量避免使用<代码> PrimTf<代码>,而不是使用< C++ >代码> >。呵呵,谢谢你的回复。我猜:A&b;b、 刚毛;可以在类B内部调用,因为我不想将它们公开给main?@VineetGolchha当然可以将初始值设定项代码放在其他地方。