我有一MainWindow
堂课
class MainWindow : public QMainWindow
{
customClass * obj;
public:
void foo(bool);
}
这是我的自定义类:
class customClass
{
void foo1(bool);
}
现在,我想foo()
在foo1()
.
怎么做?
我有一MainWindow
堂课
class MainWindow : public QMainWindow
{
customClass * obj;
public:
void foo(bool);
}
这是我的自定义类:
class customClass
{
void foo1(bool);
}
现在,我想foo()
在foo1()
.
怎么做?
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_;
}
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.
一种方式 - 使用依赖注入模式:link
struct A;
struct B
{
B( A& a );
void foo1()
{
m_a.foo();
}
private:
A& m_a;
}
struct A
{
void foo(){}
B m_b;
}