Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting around Qt's templating restrictions
    text
    copied!<p>I want to write a simple flexible ftp server in C++ that can be parametrized with a class to handle the user (check login and password, deliver files etc.) supplied on server initialization.</p> <p>So I came up with this neat (so I thought) idea:</p> <pre class="lang-c prettyprint-override"><code>class FtpDelegate { public: FtpDelegate() {} virtual ~FtpDelegate() {} virtual bool login(QString username, QString password) = 0; // ... }; class DummyDelegate : public FtpDelegate { public: virtual bool login(QString username, QString password) { return true; } }; template&lt;class Delegate&gt; class FtpServer : public QObject, Derived_from&lt;Delegate, FtpDelegate&gt; { Q_OBJECT public: explicit FtpServer(const QHostAddress &amp;address = QHostAddress::Any, quint16 port = 21, QObject *parent = 0); public slots: void newConnection(); private: QTcpServer *server; QHostAddress address; }; template &lt;class Delegate&gt; void FtpServer&lt;Delegate&gt;::newConnection() { FtpDelegate *delegate = new Delegate(); new FtpConnection (delegate, server-&gt;nextPendingConnection(), address, this); } class FtpConnection : public QObject { Q_OBJECT public: explicit FtpConnection(FtpDelegate *delegate, QTcpSocket *socket, const QHostAddress &amp;address, QObject *parent = 0); public slots: void newDataConnection(); private: QTcpSocket *socket; QTcpServer *dataServer; // needed to transfer data to user QTcpSocket *dataSocket; }; // server initialization FtpServer&lt;DummyDelegate&gt; ftpServer(); </code></pre> <p>and then (you probably saw that coming) bam!</p> <pre><code>Error: Template classes not supported by Q_OBJECT </code></pre> <p>it is likely that there are other errors or misconceptions too as I am only starting to learn the C++ template mechanism (and Qt as well).</p> <p>My question is: what is the best way to make it work without using ugly hacks like passing function pointers or needing to create a factory implementation for each concrete FtpDelegate's derived class. Maybe there's some clever design pattern I just can't see. Eventually I can rewrite the network mechanism to boost if it is the best option.</p>
 

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