Skip to main content
gamedev.net
🔒 Locked

Overloading << question

Started by fallenang3l Apr 13, 2002 at 1:30 PM 4 replies 700+ views
Original Post
fallenang3l
fallenang3l
I have a problem with teaching cout how to recognize my objects. Let's take a very basic example:
#include <iostream>
using namespace std;

class Example
{
private:
	int m_x;
public:
	Example(int = 1);
	friend void operator<<(ostream &, Example);
};

int main()
{
	Example overloading;
	cout << overloading;

	return 0;
}

Example::Example(int i)
{
	m_x = i;
}

void operator<<(ostream & os, Example e)
{
	os << e.m_x;
}
  
The above snippet of code works as intended, but being willing to experiment, I made one modification to it and it broke. Namely, I removed the & reference-to operator from the operator<< parameter list so that the whole function header and the prototype read like this: void operator<<(ostream os, Example e) Can anyone explain why the code works with ostream & but not with plain ostream? Thanks. [Edit] Forgot to add, here's the error message: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Example' (or there is no acceptable conversion). [edited by - fallenang3l on April 13, 2002 2:42:23 PM]
Oluseyi
Oluseyi
quote:
Original post by fallenang3l
Can anyone explain why the code works with ostream & but not with plain ostream? Thanks.

References. ostream & takes the address of the specified object and allows the method to operate on and change it (and is more efficient since an address is only the same size as a system word). ostream passes a copy of the object, which is thrown away so the changes have no external effect.

Furthermore, your operator << should return an ostream reference as well, to allow for chaining:
cout << myobj1 << myobj1 << ... << endl; 


A void method makes the above impossible.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
fallenang3l
fallenang3l
quote:
Original post by Oluseyi
References. ostream & takes the address of the specified object and allows the method to operate on and change it (and is more efficient since an address is only the same size as a system word). ostream passes a copy of the object, which is thrown away so the changes have no external effect.

Furthermore, your operator << should return an ostream reference as well, to allow for chaining:
cout << myobj1 << myobj1 << ... << endl;    


A void method makes the above impossible.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!




So why is that when I remove the actual code from the operator<< function (making it an empty, do-nothing function), I still get the same error. According to your explanation, it should work this time since I'm not modifying any ostream object at all. Or maybe I misunderstood you.

[edited by - fallenang3l on April 13, 2002 3:06:42 PM]
SabreMan
SabreMan
Please show the *exact* code that causes the error.
fallenang3l
fallenang3l
quote:
Original post by SabreMan
Please show the *exact* code that causes the error.



#include <iostream>
using namespace std;

class Example
{
private:
int m_x;
public:
Example(int = 1);
friend void operator<<(ostream, Example);
};

int main()
{
Example overloading;
cout << overloading;
return 0;
}

Example::Example(int i)
{
m_x = i;
}

void operator<<(ostream os, Example e)
{
}


I just want to know why it doesn''t work without ostream &.
fallenang3l
fallenang3l
Dammit, I''m a dumbass. I figured it out after rereading Oluseyi''s post a few times. Thanks man .

Topic Locked

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

Sign in to reply to this topic.