开发者

c++ cross access to classes methods

I'm not very experienced in cpp (used to work in python). I have the following problem:

1) I have a main class A (a window) and the methods m_A1 and m_A2

2) I have a little class B (a dialog) with a callback m_B1

3) the class B is instantiated and destroyed inside of m_A1

4) from the callback m_B1 I need to call m_A2

I tried to give to B reference to the instance of A (with 'this') but this solution that worked in python here doesn't.

I tried to declare the class B inside of A to have the methods of A accessible inside of B but I can't understand how to write the code of the methods of B, writing for example the class constructor of B would be A::B::A::B() but gives syntax errors.

Her开发者_开发问答e's some code:

class Centrino
{
public:
    Centrino();
    virtual ~Centrino();

    Gtk::Window  *mp_window;

protected:
    ...
    bool  on_window_key_press(GdkEventKey *event);
    void  io_process_incoming_command(char *in_str_complete);
    ...
};

class DebugDialog : public Gtk::Dialog
{
public:
    DebugDialog(const char *title, Gtk::Window& parent, bool modal);
    virtual ~DebugDialog() {};

protected:
    void  on_button_send_clicked();
    ...
};

void  Centrino::io_process_incoming_command(char *in_str_complete)
{
    ...
}

bool  Centrino::on_window_key_press(GdkEventKey *event_key)
{
    if(event_key->state & GDK_CONTROL_MASK)
    {
        if((event_key->keyval == GDK_KEY_d) || (event_key->keyval == GDK_KEY_D))
        {
            DebugDialog  dialog("Debug Dialog", *mp_window, true);
            int  response = dialog.run();
        }
    }
    ...
}

void  DebugDialog::on_button_send_clicked()
{
    Glib::ustring  entry_content = m_entry.get_text();
    io_process_incoming_command(entry_content.c_str());
}

Centrino is the class that I called A, DebugDialog is the class that I called B. From DebugDialog:: on_button_send_clicked() I need to call Centrino:: io_process_incoming_command(). The scope of the class DebugDialog instance is inside of Centrino:: on_window_key_press(). Can anybody point me to an example? Thanks in advance.


Add a Centrino reference in DebugDialog attributes and provide it in DebugDialog contructor. Declare Centrino::io_process_incoming_command() method public and invoke it from DebugDialog::on_button_send_clicked() method:

class Centrino
{
public:
    Centrino();
    virtual ~Centrino();

    Gtk::Window  *mp_window;

    void  io_process_incoming_command(char *in_str_complete);
protected:
    ...
    bool  on_window_key_press(GdkEventKey *event);

    ...
};

bool  Centrino::on_window_key_press(GdkEventKey *event_key)
{
    if(event_key->state & GDK_CONTROL_MASK)
    {
        if((event_key->keyval == GDK_KEY_d) || (event_key->keyval == GDK_KEY_D))
        {
            DebugDialog  dialog("Debug Dialog", *mp_window, true, *this);
            int  response = dialog.run();
        }
    }
    ...
}

class DebugDialog : public Gtk::Dialog
{
public:
    DebugDialog(const char *title, Gtk::Window& parent, bool modal, Centrino &centrino);
    virtual ~DebugDialog() {};

protected:
    void  on_button_send_clicked();
    ...

    Centrino &centrino_;
};

DebugDialog::DebugDialog(const char *title, Gtk::Window& parent, bool modal, Centrino &centrino) :
    ....
    centrino_(centrino)
{
    ...
}

void  DebugDialog::on_button_send_clicked()
{
    Glib::ustring  entry_content = m_entry.get_text();
    centrino_.io_process_incoming_command(entry_content.c_str());
}


The scope of DebugDialog is global, and as written, DebugDialog has no knowledge of the context in which it was created. You need to pass and save this information explicitly:

class DebugDialog : public Gtk::Dialog
{
    Centrino* myOwner;

public:
    DebugDialog( Centrino* owner, const char *title, Gtk::Window& parent, bool modal );
    virtual ~DebugDialog() {};

protected:
    void  on_button_send_clicked();
    ...
};

DebugDialog::DebugDialog( Centrino* owner... )
    : myOwner( owner )
    , ...

void  Centrino::io_process_incoming_command(char *in_str_complete)
{
    ...
}

bool  Centrino::on_window_key_press(GdkEventKey *event_key)
{
    if(event_key->state & GDK_CONTROL_MASK)
    {
        if((event_key->keyval == GDK_KEY_d) || (event_key->keyval == GDK_KEY_D))
        {
            DebugDialog  dialog(this, "Debug Dialog", *mp_window, true);
            int  response = dialog.run();
        }
    }
    ...
}

void  DebugDialog::on_button_send_clicked()
{
    Glib::ustring  entry_content = m_entry.get_text();
    myOwner->io_process_incoming_command(entry_content.c_str());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜