This topic is 4982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

// input and print integers1 and integers2
cout << "Input 17 integers:\n";
cin >> integers1 >> integers2;
cout << "After input, the arrays contain:\n"
<< "integers1:\n" << integers1
<< "integers2:\n" << integers2 << '\n';

// Overloaded input operator for class Array;
// inputs values for entire array
istream &operator>>( istream &input, Array &a )
{
for ( int i = 0; i < a.size; i++ )
input >> a.ptr[ i ];

return input;	// enables cin >> x >> y;
}

I'm learning about operator overloading right now. I was wondering what is happening when the return input line is reached. I know that input, being a reference for cin >> is returned but what exactly is being returned/happening?

Share on other sites
It is returning a reference to the cin object. The reason they do this is to allow you to 'stack' the operator >> calls in one statement. If you did a void operator >> instead, this line:
cin >> integers1 >> integers2;

wouldn't work because the second '>> integers2' would try to call operator >> on void.

edit: not specifically cin of course, whatever you happend to call the operator >> with.

Share on other sites
The thing to remember, TheOne1, is that a reference is essentially just another name, or even a voodo doll if you like. Once the function has completed and hits the return statement, the reference is passed back and the next >> call proceeds in exactly the same manner as the first.

If the return type were a value, it would have had to be constructed, but in this case, the reference (which is probably still on the stack) is simply copied to eax, and then onto the stack as the next parameter.

Share on other sites
ahhhh. Thanks guys!

Share on other sites
a similar question
	// print integers1 size and contents	cout << "Size of array integers1 is "		 << integers1.getSize()		 << "\nArray after initialization:\n"		 << integers1 << '\n';// Overloaded output operator for class Arrayostream &operator<<( ostream &output, const Array &a ){	int i;	for ( i = 0; i < a.size; i++ ) {		output << setw( 12 ) << a.ptr[ i ];		if ( ( i + 1 ) % 4 == 0 )	// 4 numbers per row of output			output << endl;	}	if ( i % 4 != 0 )		output << endl;	return output;	// enables cout << x << y;}

From the knowledge of the first answer I recieved, I know that return output returns a reference to enable the cascading of cout << x << y;. But when it returns and the function ends, where is the reference returned? Is it returned for the newline so that the newline works/can be printed?

Share on other sites
normally when a function returns a value that isn't assigned to anything it gets taken off the stack and is lost. I believe though taht cin and cout are Singletons to iostream classes. So after the initial instantiation, they float around till the end of the program. If you want to knwo more about Singletons there is a discussion about them somewhere else on the board.

Share on other sites
Imagine it like this. Since each << call has equal precedence, the calls get evaluated left to right. The compiler sees the << call, so it partially generates the correct code for that. Next, it checks if another function (be it a regular function or simple assignment) uses the return type. In this case, << uses the last <<'s return as an argument, so the compiler passes it to the next function, and continues to generate code and so on.

The important thing to realise is that cout does nothing, it is simply a reference. operator<< is doing all the work, as it is a function that takes an argument from the left, an argument from the right, and in this case prints to the console.

It's exactly the same as

int a = 5 + 7 + 4;

where the following calls are made to
int operator+(int, int)

operator+(5, 7) returns 12
operator+(12, 4) returns 16

and then assignment.

• 9
• 50
• 11
• 17
• 11