Qt World Summit: Submit your Presentation

Can we use MACROS for a class declaration ?

  • For example,

    #define CLASSNAME class A { public: A() {} static void SomeFunc(){} };

    Is this the right way to do ?
    I have tried doing this but after this decalration i cannot see/access class A in the intellisense.
    and i also i want to access A::SomeFunc() in other place.

  • You need to separate class A into a header containing the declaration and a file containing the implementation. In the other class implementation that needs A::SomeFunc() you should include the new header.

    // a.h
    #ifndef CLASSA
    #define CLASSA
    class A {
    static void SomeFunc();
    #endif // CLASSA
    // a.cpp
    A::A() { }

    void A::SomeFunc() { }
    // b.cpp
    #include "a.h"


    This is generic C++ and not Qt related.

  • You could remove the A.cpp file if you define your functions in your class definition. (this is done by QtDesigner in the _ui.h files). This way you will only need to include the header file.
    When you want to use the function you will also need an object (else the function should be made static and placed in a cpp file!)
    So the code ChrisW67 mentioned will only work with the A::SomeFunc() call being static!!

Log in to reply