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
  • My K&R doesn't say anything about it being illegal.

    Legality is not the problem. Clean design and code maintainability are.

    Extern-declaring other modules' objects in your own is an invitation for problems that are notoriously hard (read: expensive!) to debug, because it robs the compiler and most other tools of any opportunity to check the correctness of that declaration.

    Declarations of global objects are part of the defining modules' job. Duplicating that work can only result in two kinds of results: either your re-declaration is equivalent to the original one --- then it's a wast of time to write it down. Or it's not equivalent --- then you're in undefined behaviour land, and you really don't want to go there.

    In the end, nothing good ever comes from writing "extern" in a C file.

Reply
  • My K&R doesn't say anything about it being illegal.

    Legality is not the problem. Clean design and code maintainability are.

    Extern-declaring other modules' objects in your own is an invitation for problems that are notoriously hard (read: expensive!) to debug, because it robs the compiler and most other tools of any opportunity to check the correctness of that declaration.

    Declarations of global objects are part of the defining modules' job. Duplicating that work can only result in two kinds of results: either your re-declaration is equivalent to the original one --- then it's a wast of time to write it down. Or it's not equivalent --- then you're in undefined behaviour land, and you really don't want to go there.

    In the end, nothing good ever comes from writing "extern" in a C file.

Children