How to pass a function pointer that points to constructor?

前端 未结 7 1997
陌清茗
陌清茗 2020-11-29 06:45

I\'m working on implementing a reflection mechanism in C++. All objects within my code are a subclass of Object(my own generic type) that contain a static member datum of ty

7条回答
  •  天涯浪人
    2020-11-29 06:52

    Using Qt, you can call a constructor with Qt reflection mechanisms (QMetaObject) if you declare the constructor as Q_INVOKABLE (nothing more to do than that).

    class MyClass : public QObject {
       Q_OBJECT
    public:
       Q_INVOKABLE MyClass(int foo);
       MyClass *cloningMySelf() {
         return metaObject()->newInstance(Q_ARG(int, 42));
       }
    };
    

    I'm not sure you will want to embed Qt just for that feature ;-) but maybe you would like to have a look on the way it does that.

    http://doc.qt.io/qt-5/metaobjects.html#meta-object-system

提交回复
热议问题