Since the data type of a queue is arbitrary, we also have the ability to pass data by reference instead of by value. This works in a similar way to passing arguments to a function by reference.
Passing data through queues by reference
When to pass by reference
Since a queue will make a copy of whatever it is holding, if the data structure being queued is large, it will be inefficient to pass it around by value:
- Sending and receiving from queues forces a copy of the queue element each time.
- The resulting queue gets very large for large data items if large structures are queued.
So, when there are large items that need to be queued, passing the items by reference is a good idea. Here's an example of a larger structure...