Reputation: 367
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?
Upvotes: 3
Views: 450
Reputation: 42888
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();
Upvotes: 5
Reputation: 11950
Reservation *(&getReservations())[30];
should do it for you.
Please remember of dangling references and pointer management.
Upvotes: 4
Reputation: 7638
I would recommend you to use modern C++:
using ReservationArray = std::array<Reservation*, 30>;
ReservationArray _availability;
and to return that as:
ReservationArray& getReservations()
Upvotes: 1