Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return array of pointers by reference

Inside one of my classes i've got a

Reservation * availability[30] = {nullptr};

(which I of course initialize later with some values).

Nevertheless, I've got a getReservations() function which is supposed to return a reference to that array of 30 elements, so that it can be used like:

getReservations()[i] ...

How should I declare that function?

like image 544
Jue Debutat Avatar asked Aug 16 '26 19:08

Jue Debutat


1 Answers

The syntax for declaring a function that returns the array by reference is this:

Reservation * (& getReservations())[30];

Of course, as you can see, you shouldn't use this in real life. Instead do it with a type alias:

using Reservations = Reservation * [30];

Reservations & getReservations();

Or, because arrays aren't bounds-checked anyway, just return a pointer which you can then index like an array:

Reservation * getReservations();
like image 108
emlai Avatar answered Aug 19 '26 08:08

emlai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!