Skip to main content
gamedev.net
🔒 Locked

overloading question

Started by TheOne1 5 replies 500+ views
Original Post
TheOne1
TheOne1

// 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';

// The overloaded function
// 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?
----Me: So do you know any computer languages?Friend: Ummm....yeah, I used to know l337 talk.Me: ok....
Kibble
Kibble
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.
Chris Hare
Chris Hare
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.
TheOne1
TheOne1
ahhhh. Thanks guys!
----Me: So do you know any computer languages?Friend: Ummm....yeah, I used to know l337 talk.Me: ok....
TheOne1
TheOne1
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?

Thanks in advance.
----Me: So do you know any computer languages?Friend: Ummm....yeah, I used to know l337 talk.Me: ok....
Chris Hare
Chris Hare
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.