This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Global Class Decleration

Hi,

I am using uVision4 for LPC3250.

I declare my class in my header file and define its functions in the corresponding cpp file.

My question is this, how can I reach the object of this class when I create it in another file.

For example, in a.cpp:

MyClass MyObject;

in b.cpp

MyBoject.value = 5;

I want my object to be global.

Thanks for your helps.

Parents
  • In your "b.cpp", add:

    extern MyClass MyObject;
    

    No, don't do that. Never write "extern" in a module's implementation file. Extern declarations of globally available items belong into the interface header file of the module defining them, not in the implementation file of modules using them.

    The basic rule is: no extern in *.c/*.cpp files, and no static in *.h files.

Reply
  • In your "b.cpp", add:

    extern MyClass MyObject;
    

    No, don't do that. Never write "extern" in a module's implementation file. Extern declarations of globally available items belong into the interface header file of the module defining them, not in the implementation file of modules using them.

    The basic rule is: no extern in *.c/*.cpp files, and no static in *.h files.

Children