I am a bit unsure of how to use the values returned by front()
function from std::queue
library. On the website cplusplus.com , it says the function returns a reference.
However, when I looked at the actual usages, it seems like the function is used directly to get the value.
For example, by following these steps
std::queue<int> myqueue;
myqueue.push(30);
The return value should be stored as follow:
int &some_number = myqueue.front();
So how come this is possible:
std::cout << "myqueue.front() returns " << myqueue.front()
//myqueue.front() returns 30
If it's returning reference to value, doesn't that mean it's returning the location where the value is stored at, and not the value itself? So how come printing the location prints out the value not the address of the integer.
Also, following this logic, does it mean these also work?
Scenario 1
std::queue<int> myqueue;
myqueue.push(30);
int new_number = myqueue.front();
new_number
's value will be 30
Scenario 2 - (someStruct is a defined struct)
std::queue<someStruct*> myqueue;
myqueue.push(someStruct* abcPtr); \\abcPtr has been initialized
someStruct* edfPtr= myqueue.front();
edfPtr
will be pointing to the same location abcPtr
is pointing too.
if so, then what's the point of passing the value returned by std::queue::front
as reference. Does anyone know the logic behind the usage?