简体   繁体   中英

C++ Alternative to a template class in a Qt plugin interface

I have trouble compiling a qt plugin with a generic member function and variable. The idea is: I have a plugin A1 which uses other plugins of unspecified types T (other interfaces). A1 implements the interface (abstract class) A. In AI need a function which passes the other plugins.

It looks like this:

template <typename T>
class A {
public:
  void setPlugins(QList<T*>* plugins)
  {
    plugins_ = plugins;
  }
private:
  QList<T*>* plugins_;
};

I hope I have understood templates right but I think this should normally work. The problem is now, that it seems to be not possible to define the plugin interface (A) as a template class with Qt's plugin concept. Is there another way to do what I want?


EDIT: I prefer a solution without RTTI.

I'll try to answer...

In Qt every plug-in class should be inherited from QObject.
Thus, you may drop templates and use list of pointers to QObjects to store the plug-ins:

QList<QObject*> plugins_;  

Yet in this case you'll lose type information and will need to somehow deduce the type of plug-in from QObject pointer later on, when you need to use a specific plug-in from the list as a plug-in.
QObject is a polymorphic type so you can use dynamic_cast to determine the type of the exact plug-in when you need to. Hope this helps.

One problem is that A isn't a class, but a template for a class. You will probably have to use a concrete A<x> for your interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM