1

I have some immutable global objects like so:

const Vehicle Car   = ...;
const Vehicle Truck = ...;
...

I need to make "aliases" to these objects, i.e., additional names which refer to the same object.

This seems to work:

const Vehicle& Camion = Truck;

Is it allowed and will it blow up in my face somehow? Will using Camion be the same as using Truck, including the object address?

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
  • Just a side-note, names in all caps are, by convention, reserved for macros. Don't use them for anything else. – Aykhan Hagverdili May 05 '19 at 17:14
  • @Ayxan - it's _one_ convention, but naming non-macro constants in all-caps is widespread in C and C++. I agree that whether it is a _good idea_ is debatable though. If you have any link to the C++ coding convention I'd like to see it, because I've been searching for it and I'd like to setting on whatever the standard endorsed conventions are. – BeeOnRope May 05 '19 at 17:18
  • 1
    C++ core guidlines says [Don't use ALL_CAPS for enumerators](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#enum5-dont-use-all_caps-for-enumerators), and the reasoning given is "Avoid clashes with macros." – Aykhan Hagverdili May 05 '19 at 17:38
  • And I swear I've seen something along the lines of "Some programmers use all capitals for const variables but those names are conventionally reserved for macros" in one of Mayers's books, but I couldn't find it strangely. – Aykhan Hagverdili May 05 '19 at 17:40
  • @Ayxan - ok, fair enough, I do try to follow the core guidelines even though they are in their infancy. I would point out that the reason they have that guideline is because using all caps for enumerations (and constants) is common. If no one did it, they wouldn't need to mention it. I also admit that I may have started doing it after writing some Java for a while :p. – BeeOnRope May 05 '19 at 17:40

2 Answers2

3

Yes, this is fine.

Reference declaration

Declares a named variable as a reference, that is, an alias to an already-existing object or function.

This is what references are for. They are essentially another name for the same thing. And yes, taking address of a reference gives you the address of the actual variable it is referencing.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
1

Yes, you it will be same because the same variable points the same memory address and change in memory address can effect both the variable. https://www.youtube.com/watch?v=Zl-JLUOuyGI