Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To face compile-time dependencies there are two methods:</p> <ol> <li>Forward declare the classes you need. </li> <li>Use an interface to break the cyclic dependency. </li> </ol> <h2>Forward declarations</h2> <p>In your <code>PluginManager.h</code> file you just write</p> <pre><code>class Main; </code></pre> <p>at the top of you file in order to forward declare <code>Main</code>. Then you declare the constructor of the <code>PluginManager</code> as</p> <pre><code>PluginManager( Main * parent ); </code></pre> <p>In the implementation file of the <code>PluginManager</code> you need to include the header which defines the <code>Main</code> class then. </p> <h2>Interfaces</h2> <p>The second option uses an interface avoids the cyclic dependency alltogether. It works like this:</p> <pre><code>class MainInterface : public QObject { Q_OBJECT public: MainInterface( QObject * parent ) : QObject(parent) {} virtual ~MainInterface() {} virtual void someFunc1() = 0; virtual void someFunc2() = 0; // ... }; class PluginManager : public QObject { Q_OBJECT public: PluginManager( MainInterface * parent = 0 ) : QObject(parent) { /* ... */ } // ... other functions ... }; class Main : public MainInterface { public: Main( QObject * parent = 0 ) : MainInterface(parent) {} virtual void someFunc1(); virtual void someFunc2(); // ... }; </code></pre> <p>The dependency graph now looks like this</p> <pre><code>MainInterface PluginManager A A instead of A | | | | V Main PluginManager Main </code></pre> <h2>Your choice</h2> <p>What alternative you want to use is your choice. If the two classes work together as an indivisible part of your program then use the easier approach of forward declaring. If you want to have these components decoupled and avoid dependencies as much as possible, then use the second approach. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload