We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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.
Why not?
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.
Thanks for the concise reasoning.
T...
Just because it's not illegal doesn't mean to say that it is desirable!
eg, goto is entirely legal, but rarely desirable.
To see this taken to the extreme, just look at the Obfuscated 'C' Code Contest...!
http://www.ioccc.org/