Search code examples
c++iosblackberry-10

NSClassFromString equivalent for BlackBerry-10


Does NSClassFromString from iOS have an equivalent in the BlackBerry 10 SDK?


Solution

  • Yes, there is an equivalent to NSClassFromString provided you use the Qt meta type system.

    You'll need to use to Q_DECLARE_METATYPE macro and qRegisterMetaType:

    class UserClass : public Superclass {
        ...
    }
    
    Q_DECLARE_METATYPE(UserClass)
    
    
    int main(int argc, char *argv[]) {
        qRegisterMetaType<UserClass>("UserClass");
    
        ....
    }
    

    Then you can create an instance of your class with the following code:

    int id = QMetaType::type("UserClass");
    UserClass *instance = (UserClass*)(QMetaType::construct(id));
    

    I guess this approach to create objects is used by QML as well, e.g. in the attachedObjects section.