<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Interface, abstract class, QObject and concrete class]]></title><description><![CDATA[<p dir="auto">I know that the title seems a little bit confusing but I'm going to explain.</p>
<p dir="auto">I was reading a few codes here and I saw something curious:</p>
<p dir="auto">Imagine that I have an interface called <code>FooInterface</code>, an abstract class called <code>AbstractBar</code> that inherits <code>FooInterface</code> and a concrete class called <code>Biz</code> that inherits <code>AbstractBar</code>, it would be something like this:</p>
<pre><code>class FooInterface
{
    virtual ~FooInterface() {};
};
Q_DECLARE_INTERFACE(FooInterface, "FooInterface/1.0")

class AbstractBar : public QObject, public FooInterface
{
    Q_OBJECT
    Q_INTERFACES(FooInterface)
};

class Biz : public AbstractClass
{
    Q_OBJECT
};
</code></pre>
<ol>
<li>I wanted to know the usage for the <code>QObject</code> in that case.</li>
<li>I know that Qt has a way to prevent memory leaking passing the parent to QObject, in that case we don't have a constructor for that, an instance of <code>Biz</code> would leak without deleting?</li>
<li>What if <code>AbstractBar</code> had a constructor with the <code>QObject</code> parenting, an instance of <code>Biz</code> would still leak?</li>
<li>This example was taken from a system with plugins, <code>QPluginLoader</code> would take the instance as parent and delete it afterwards or it would be mess up?</li>
<li>What is the best way to create this kind of structure? Interface, abstract class and concrete class in Qt?</li>
</ol>
]]></description><link>https://forum.qt.io/topic/75308/interface-abstract-class-qobject-and-concrete-class</link><generator>RSS for Node</generator><lastBuildDate>Mon, 06 Apr 2026 00:03:19 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/75308.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 19 Jan 2017 18:20:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Interface, abstract class, QObject and concrete class on Thu, 19 Jan 2017 19:43:32 GMT]]></title><description><![CDATA[<ol>
<li>I'm not sure I understand what you mean. Inheriting QObject makes the class part of the Qt's <a href="http://doc.qt.io/qt-5/object.html" target="_blank" rel="noopener noreferrer nofollow ugc">object model</a>. The usage is whatever you want it to be.</li>
<li>Class not having a constructor with parent parameter doesn't make it leak memory. Consider these examples:</li>
</ol>
<pre><code class="language-cpp">{ //scope
   Biz foo;
} //end of scope, foo is released, no leak
</code></pre>
<pre><code class="language-cpp">Biz* foo = new Biz();
delete foo; //deleted, no leak
</code></pre>
<pre><code class="language-cpp">QObject* parent = new QObject();
Biz* foo = new Biz();
foo-&gt;setParent(parent);
delete parent; //parent deletes its child, no leak
</code></pre>
<pre><code class="language-cpp">Biz* foo = new Biz();
connect(qApp, &amp;QApplication::aboutToQuit, foo, &amp;Biz::deleteLater); //deleted via connection, no leak
</code></pre>
<p dir="auto">Providing a constructor with parent is just a convention. It's a good convention and you should follow it but it's not strictly necessary.<br />
3. Even if you do provide such constructor no one prevents you to pass a <code>nullptr</code> there. Providing this constructor does not automagically make it safe. Someone needs to pass that parent there, but (as seen above) it's not the only way to manage lifetime.<br />
4. <code>QPluginLoader</code> loads a plugin and provides a root component via <code>instance()</code> method. What do you mean by "would take the instance as parent"?<br />
5. There's no "best way". There might be "good way for particular use case" but, as with all programming, there's no holy grail. If the example you gave works for you it works for you ;)</p>
]]></description><link>https://forum.qt.io/post/370512</link><guid isPermaLink="true">https://forum.qt.io/post/370512</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Thu, 19 Jan 2017 19:43:32 GMT</pubDate></item></channel></rss>