2011-09-30 142 views
0

我不是很熟悉cpp(用於在python中工作)。 我有以下問題:C++跨類訪問方法

1)I有一個主類A(的窗口)和方法m_A1和m_A2

2)I有一點B類(對話)具有的回調m_B1

3)類B從回調m_B1我需要調用m_A2

我試圖給到B參考A的實例實例化,並破壞m_A1

4)的內側(與「這')但這個解決方案,工作我蟒蛇這裏沒有。

我試圖在A中聲明B類的內部方法,但是我無法理解如何編寫B方法的代碼,例如編寫B的類構造函數是 A :: B :: A :: B()但給出語法錯誤。

下面是一些代碼:

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()); 
} 

迅馳就是我稱爲A類,DebugDialog是我叫B. 從DebugDialog類:: on_button_send_clicked()我需要調用迅馳:: io_process_incoming_command( )。 DebugDialog類實例的範圍在Centrino :: on_window_key_press()中。 有人能指點我一個例子嗎?提前致謝。

回答

0

添加迅馳參考DebugDialog屬性和DebugDialog構造器提供。聲明Centrino :: io_process_incoming_command()方法public並從DebugDialog :: on_button_send_clicked()方法調用它:

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()); 
} 
0

DebugDialog的範圍是全球性的,並且如所撰寫的,DebugDialog具有 沒有關於其創建的上下文的知識。你需要明確地傳遞 並保存此信息:

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()); 
}