0

我有一MainWindow堂课

class MainWindow : public QMainWindow
{
    customClass * obj;
public:
    void foo(bool);
}

这是我的自定义类:

class customClass 
{
    void foo1(bool);
}

现在,我想foo()foo1().

怎么做?

4

3 回答 3

1

You can make the constructor of your customClass take a pointer to a MainWindow which it stores in a member variable for later use.

class customClass 
{
public:
  customClass(MainWindow* mainWindow)
  : mainWindow_(mainWindow)
  {
  }

  void foo1(bool b) {
    mainWindow_->foo(b);
  }

private:
  MainWindow* mainWindow_;
}
于 2013-04-22T20:59:18.527 回答
0

You can make your MainWindow implements singleton pattern (if it's applicable to your design), then you can directly get an instance from any place you like.

于 2013-04-22T17:53:09.730 回答
0

一种方式 - 使用依赖注入模式:link

struct A;
struct B
{
  B( A& a );

void foo1()
{
  m_a.foo();
}

private:
  A& m_a;
}

struct A
{
  void foo(){}
  B m_b;
}
于 2013-04-22T17:37:06.940 回答