Overloading >> instream

I'm writing a class that involves a queue, and I want to overload the instream. I know that for a more simple program, the correct way to overload the instream would be to just assign the input values to the private data members in the class and return. One example would be, with a class such as Distance that has private data members for feet and inches:

1
2
3
4
5
6
istream& operator>>(istream &input, Distance &d)
{
	input >> d.feet >> d.inches;

	return input;
}


For my class, the user will input a string (of numbers) through instream, and I will take that string and break it up into chars. Each char will become the "item" of a node that will then be inserted a queue. So far, what I intend to do is:

1
2
3
4
5
6
7
8
9
10
11
12
13
istream& operator>>(istream &in, LongInt &rhs)
{
	rhs.digits.clear(); // clears the original queue
	string input; // string to hold input from user
	in >> input;

	// Here I will iterate over the string with a for each loop
        // I will take each char, and insert it into a queue
        // The queue program is correct, so assume passing my char
        // to the queue program will successfully create a node for the queue

	return in;
}


So far, it appears to be working, but I am unsure if this is the correct way to implement overloading >> in this situation. I don't really understand why I am returning "in" as well, as it appears to be useless as I receive the input from the user and successfully create the queue before returning in occurs. Does anyone know if this is the correct way to implement overloading <<?
Looks like an OK start to me (assuming there are no spaces in your "string of numbers".)

I don't really understand why I am returning "in" as well, ...

operator>> returns a reference to the input istream (in in your case) to enable operator chaining, e.g.

cin >> rhs >> d >> t >> s;

(operator>> returns the istream so the next >> can call it.)

Andy


Last edited on
Ah I see. Yup, my algorithm already handles spaces or anything that isn't a number. I didn't understand the returning in, but now I do!
Topic archived. No new replies allowed.