I have an object that has an array attribute, say (*obj).some_array;, where some_array is an integer array.
I know that we can alias simply like so:
int test = 10;
int &alias = test;
However, how can I alias to an array of an object, similarly to above but for obj's some_array?
If obj->some_array is of type int[LEN] you can just do:
int (&alias)[LEN] = obj->some_array;
To understand this weird syntax you could use theClockwise/Spiral Rule, a way you could read declarations you're not familiar it (see here).
int &alias[LEN] (which is the same as int &(alias[LEN]) would be an array of LEN references to int.
int (&alias)[LEN] is, instead, a reference to an array of LEN ints
Note that what you call alias is called a reference in C++!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With