0

I have a MainWindow class

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

Here is my customClass:

class customClass 
{
    void foo1(bool);
}

Now, I want to call foo() method in foo1().

How to do that?

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
shivshnkr
  • 1,435
  • 1
  • 13
  • 19

3 Answers3

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_;
}
Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
0

One of way - using of dependency injection pattern: link

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

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

private:
  A& m_a;
}

struct A
{
  void foo(){}
  B m_b;
}
Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
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.

evilruff
  • 3,947
  • 1
  • 15
  • 27