Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aliasing to an array

Tags:

c++

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?

like image 925
apple Avatar asked Mar 22 '26 16:03

apple


1 Answers

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++!

like image 85
peoro Avatar answered Mar 25 '26 04:03

peoro