none
How to get instance of class from Class in External file? RRS feed

  • Question

  • Hello, suppose I have the following 3 files, I want to declare a variable instance of MyContextReactor class in call.cpp

    MyClass.cpp

    #include "function.h"

    class MyContextReactor : public AcEdInputContextReactor { public: void beginGetPoint( const AcGePoint3d* pointIn, const char* promptString, int initGetFlags, const char* pKeywords);

    ....

    private:

    ....

    }

    //my function

    MyContextReactor::beginGetPoint...

    ...

    Call.cpp

    #include "function.h"

    MyContextReactor cls;

    my questions is:

    in function.h file

    do I have to declare all the function and variables of function.h again? (If so, I have to copy it twice?)

    function.h

    #ifdef...

    ...

    #endif

    class MyContextReactor : public AcEdInputContextReactor { public: void beginGetPoint( const AcGePoint3d* pointIn, const char* promptString, int initGetFlags, const char* pKeywords);

    ...

    };

    Is there any shorter method? please help me.
    thanks a lot!


    • Edited by Khanh01 Friday, February 14, 2020 6:51 PM
    Friday, February 14, 2020 6:47 PM

Answers

  • Include the class declaration for class MyContextReactor in function.h.

    in myclass.cpp -

    #include "function.h"

    ....code for the definition of MyContextReactor constructors, destructor and member functions

    in call.cpp

    #include "function.h"

    MyContextReactor cls;  // Instance of MyContextReactor object



    • Edited by RLWA32 Friday, February 14, 2020 7:24 PM fix typo as per WayneAKing comment
    • Marked as answer by Khanh01 Saturday, February 15, 2020 6:27 AM
    Friday, February 14, 2020 7:09 PM

All replies

  • Include the class declaration for class MyContextReactor in function.h.

    in myclass.cpp -

    #include "function.h"

    ....code for the definition of MyContextReactor constructors, destructor and member functions

    in call.cpp

    #include "function.h"

    MyContextReactor cls;  // Instance of MyContextReactor object



    • Edited by RLWA32 Friday, February 14, 2020 7:24 PM fix typo as per WayneAKing comment
    • Marked as answer by Khanh01 Saturday, February 15, 2020 6:27 AM
    Friday, February 14, 2020 7:09 PM

  • in class.cpp

    #include "function.h"

    MyContextReactor cls;  // Instance of MyContextReactor object

    Possible typo alert.

    To be consistent with the OP's code example perhaps this should read:

    in call.cpp

    - Wayne

    Friday, February 14, 2020 7:16 PM
  • Thanks, typo corrected above.
    Friday, February 14, 2020 7:23 PM
  • Hi, Thanks experts, it is working for me.
    Saturday, February 15, 2020 6:29 AM