2011-09-30 103 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()); 
}