-2

I have this, a header file with class declaration and one global object instance:

struct ClassName
{
};
extern ClassName ClassObject;

But in CPP codes I'd like to access it via shorter name, for example CO instead of ClassObject

Ideally I'd like to do something like typedef ClassObject CO; but it's not supported

I don't want to use #define CO ClassObject because it's dangerous.

I don't want to use

extern ClassName &CO;
ClassName &CO=ClassObject;

Because that would decrease performance, accessing CO would have to lookup the address value first, while ClassObject wouldn't have to. So using ClassObject would be faster than CO, but I need same performance.

Tried with a union

union
{
   ClassName ClassObject;
   ClassName CO;
};

but it requires union to be static, so can't declare in header and share across other CPP files.

So there's no solution for this?

5
  • "Because that would decrease performance, accessing CO would have to lookup the address value first, while ClassObject wouldn't have to." Two questions: why do you believe that this is true? That is, why do you think that the compiler wouldn't be able to realize that the reference will always refer to that specific variable and therefore not do any more work than would be needed for ClassObject? Also, why do you think that the access time of this object will matter for your application's performance? Apr 5, 2020 at 3:10
  • Because only this "extern ClassName &CO;" would be in the header. So when compiling CPP files, they wouldn't know to what CO actually points.
    – Esenthel
    Apr 5, 2020 at 3:18
  • "Also, why do you think that the access time of this object will matter for your application's performance?" - because I'm a good programmer that writes high performance code without any sacrifices. Also it's for a real-time 3d game engine.
    – Esenthel
    Apr 5, 2020 at 3:19
  • How do you know that this will be "high performance code without any sacrifices"? Is 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)? Apr 5, 2020 at 3:27
  • "only this "extern ClassName &CO;" would be in the header" That's not what your example does. And you could always create references locally within the functions that use ClassObject sufficiently extensively to need to abbreviate it. Apr 5, 2020 at 3:49

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.