Sharing info between dialogs

From WxWiki
Jump to navigation Jump to search

This is more a C++ issue than a wx one, but comes up very often on the forum so at least we have a place to point people to...

Imagine you have ClassA and ClassB, and you'd like ClassB to be able to communicate with ClassA. The easiest way to do this is to use pointers.

Pseudo-code for ClassA :

class ClassA
{
public:

    void foo()
    {
        ClassB* someB = new ClassB();
        someB->setRelative(this);
    }

    void bar()
    {
        printf("B is communicating with me =)\n");
    }
};

Pseudo-code for ClassB :

class ClassA; //forward

class ClassB
{
    ClassA* m_relative;
    
public:

    void setRelative(ClassA* other)
    {
        this->m_relative = other;
    }
    
    void foo()
    {
        m_relative->bar(); // yeah I have a pointer to a ClassA, so I can communicate with it =)
    }
};

If you need something fancier (like communication across threads), then have a look at Inter-Thread and Inter-Process communication