implementation of >> operator in class

I'm solving an exercise in which I have to implement a string queue using classes. I have the classes StringQueue, that is the actual queue, and QueueElement. The StringQueue object points to the first QueueElement, and each QueueElement holds a string and a pointer to the next QueueElement, so the whole queue is a chain of QueueElement objects with a StringQueue object in the beginning.

The function `std::string StringQueue::dequeue()` returns the first queue element. Now I have to implement the >> operator, so that `string s; queue >> s;` is the same as `string s = queue.dequeue();`. I've already managed to make this work using pointers with this function:

1
2
3
void StringQueue::operator>> (std::string *s) {
   *s = dequeue();
}

But for this to work, I have to use the address of the string: `string s; queue >> &s;`.

How do I have to modify the function so that `string s; queue >> s;` works?
Last edited on
Use the reference:
1
2
3
void StringQueue::operator>> (std::string &s) { // Note &
   s = dequeue(); // Note: no *
}
Great, works perfectly, thanks a lot!
Topic archived. No new replies allowed.