This is an unfortunately common design malady, wherein a programmer arbitrarily defines the problem so narrowly that they define away all possible solutions to that problem.
Macros are deemed "dangerous", despite there not actually being anything especially dangerous about this particular macro; it's just a name that turns into a different name (being only two letters is conceptually dangerous, but no more so than using a 2-letter identifier). So it is disregarded, even though it is workable solution (aside from the lack of namespace scoping, but you almost certainly aren't interested in that since you want the name to be as short as possible).
References are deemed to "decrease performance", despite a lack of evidence for references being slow in this particular case nor of the performance of your application being materially affected by an indirect access to a globally accessible object. So it is disregarded, even though this is a perfectly valid use of references in C++ and is directly analogous to type aliases.
If you start from the presupposition that all of the good solutions are bad, then you're obviously not going to be left with any good solutions. And that's where you find yourself.
But really, the best solution is to avoid creating tiny aliases for variables like this to begin with. It's one thing to create aliases for complex template classes like std::vector<int, MyAllocator<int>>
and so forth, where there is a huge amount of cruft and verbiage spent on something that's much simpler than it appears. It's quite another to abbreviate an object name, particularly if the abbreviation is as short as 2 letters.
If the 2 letter name is an adequately descriptive name within this context, then that's what it should always be called. If the long name is long because it needs to be that long in order to be adequately descriptive, then odds are good that this descriptiveness will be important everywhere it gets used. And regardless of how you implement this alias, having two names for the same object in the same scope will get confusing to someone who has to read this code and figure out what it's doing. And that "someone" may well be your future self.
So just... don't do this.
only this "extern ClassName &CO;
" would be in the header. So when compiling CPP files, they wouldn't know to what CO
actually points.
If that really, really, really bothers you, C++17 offers inline
variables, which would allow you to put inline ClassName &CO = ClassObject;
in your headers.
ClassObject
? Also, why do you think that the access time of this object will matter for your application's performance?ClassObject
going to be in your processor's cache every time you access it? Is there not some more efficient way to access the data being encapsulated by this class than through a global variable? Or will the performance difference be negligible and you should focus on something that matters, like properly threading your engine (which means avoiding global state, not micro-optimizing it)?ClassObject
sufficiently extensively to need to abbreviate it.