I'm trying to create a reference to an array.
It works this way:
typedef int array_type[100];
int main() {
int a[100];
array_type &e = a; // This works
}
But then I was trying to remove the typedef
, and get the same thing working. Haven't been successful.
int main() {
int a[100];
// int[100] &e = a; // (1) -> error: brackets are not allowed here; to declare an array, place the brackets after the name
// int &e[100] = a; // (2) -> error: 'e' declared as array of references of type 'int &'
}
What is wrong with my interpreation of typedef
? And how could I remove the typedef
, and still get the same functionality.
You need to add parentheses to tell that this is a reference to array, but not an array of something. e.g.
int (&e)[100] = a;
Or use auto
or decltype
(both since C++11) to make it simpler.
auto& e = a;
decltype(a)& e = a;
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