BeeOnRope
BeeOnRope

Reputation: 64905

Global reference to global objects

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?

Upvotes: 1

Views: 93

Answers (2)

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29975

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.

Upvotes: 3

Sachin Maharjan
Sachin Maharjan

Reputation: 78

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

Upvotes: 1

Related Questions