Skip to main content
gamedev.net
🔒 Locked

overloading << operator

Started by jeeky Oct 28, 2002 at 10:36 PM 0 replies 500+ views
Original Post
jeeky
jeeky
Hi all, I am trying to overload the stream input and output operators. I will keep things to the output operator for now (<<). Here's the question: I can do this and everything is great-- #include <iostream> class foo { public: explicit foo() : m_var(0.0f) {} virtual ~foo() {} friend std::ostream& operator << (std::ostream &os, const foo& myFoo) { os << "m_var = " << myFoo.m_var << std::endl; return os; } private: float m_var; }; but I can't do this to the friend function-- static std::ostream& operator << (std::ostream &os, const foo& myFoo) { os << "m_var = " << myFoo.m_var << std::endl; return os; } This is what I get-- error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class foo' (or there is no acceptable conversion) Why not? When I have a static, the this ptr isn't passed, so things should be the same as if I had declared the function outside of the scope of the class, except I have access to the passed class members. I have ST(Friendly?)W and found that everyone uses the friend variety of implementation. I am just wondering why a static implementation doesn't work. Thanks in advance. Jeeky P.S. I can also do this-- std::ostream& operator << (std::ostream &os, const foo& myFoo) { os << "booger" << std::endl; //os << "m_var = " << myFoo.m_var << std::endl; return os; } This function was declared at the file scope (not within the class) and it works fine except for not having access to privates. Functionally, it should be identical to a static within the class. What gives? [edited by - jeeky on October 28, 2002 11:40:59 PM]
Fruny
Fruny
A friend function is implicitely not a member function, since friend means you have a non-member function that is allowed to access private data.


class foo
{
friend std::ostream& operator<<( std::ostream&, const foo& );
}

std::ostream& operator<<( std::ostream& os, const foo& f ) // NOT foo::operator<<
{
// do stuff with f's private data.
return os;
};


A static friend is therefore nonsensical, there is no this pointer anyway.

As for why you can't use a static, well, when you write std::cout << foo();, you're calling the global operator<<( std::ostream&, const foo& ), not foo::operator<<( std::ostream&, const foo& ). To call the latter, you would have to write foo::operator<<( std::cout, foo() );. (but, compare with Koenig lookup for namespaces).

It's just not the right syntax. Stick with the friend.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on October 29, 2002 2:28:11 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

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

Sign in to reply to this topic.