Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ composition best practices
    primarykey
    data
    text
    <p>I'm having troubles choosing the best solution to my problems. Currently, I'm working on my first C++ project, I'm using Qt BTW, but because I'm a .NET developer, there are some things that bit me concerning C++ :). As you all know, in a gargage collected world, such as Java or .NET, best practice is to use Dependency Injection for loose coupling classes, making it more testable and maintenable, but I have no idea what to do in C++. C++ doesn't have a GC, so we should take care about every memory allocation and that causes too much questions.</p> <p>For example, I have a class "Rule", which has a member field pointing to "Application" class:</p> <pre><code>class Rule : public QObject { public: explicit Rule(QObject *parent = 0); unsigned int id(); void setId(unsigned int id); Application* application(); void setApplication(Application* application) { m_Application = application m_Application-&gt;setParent(this); } }; </code></pre> <p>In the constructor, I'm assigning NULL to application instance variable. In setApplication, I'm assigning this as the parent to application. Application instance will be deleted automatically when Rule (the parent) is destroyed, thanks to Qt. Is this a good approach? would it be better if I used smart pointer such as QSharedPointer instead of Application*? What's your experience, what's the drawbacks, what's the best approach. I would gladly listen to your advices. Also there is another tricky part here. What if I gave that class to another developer in the team, or I published the library. The developer could easily write something like:</p> <pre><code>Application app; app.setId(1); Rule rule; rule.setApplication(&amp;app); //When rule will be destroyed, the program would crash because app is allocated on the stack. </code></pre> <p>or</p> <pre><code>Application *app = new Application(); app-&gt;setId(20); Rule *rule = new Rule(); rule-&gt;setApplication(app); Application *appToAnotherLocation = new Application(); rule-&gt;setApplication(appToAnotherLocation); // This wouldn't result in memory leak, because app is already child of rule, but if I didn't used Qt this would be a problem... probably :) </code></pre> <p>Now what about smart pointers? Is there some rules, when we should use them and when we shouldn't? I have a IRepository interface, which always returns QSharedObject instead of pointers. Is this a good approach or am I overusing it?</p> <pre><code>class IRepository { public: virtual bool save(Application &amp; application) = 0; virtual bool save(Rule &amp; rule) = 0; virtual bool save(History &amp; history) = 0; virtual bool remove(Application &amp; application) = 0; virtual bool remove(Rule &amp; rule) = 0; virtual bool remove(History &amp; history) = 0; virtual QSharedPointer&lt;Application&gt; getApplication(unsigned int id) = 0; virtual QSharedPointer&lt;Rule&gt; getRule(unsigned int id) = 0; virtual QList&lt; QSharedPointer&lt;Rule&gt; &gt; getRules(unsigned int applicationId) = 0; virtual QSharedPointer&lt;History&gt; getHistory(unsigned int id) = 0; virtual QList&lt; QSharedPointer&lt;History&gt; &gt; getHistories(unsigned int applicationId) = 0; virtual QList&lt; QSharedPointer&lt;History&gt; &gt; getHistories(unsigned int applicationId, QDateTime dateFrom, QDateTime dateTo) = 0; }; </code></pre> <p>Thanks. Your help is much appreciated. </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.
 

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