Jump to content
  • Advertisement
Sign in to follow this  
kulik

ostream and Logging, operator

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I made a simple logging subsystem called LogManager, which can redirect all logged messages onto other logs or listeners, etc. Now I want to overload operator << so I can then write:
LogManager << "Something bad happened, error number: " << 1;
I tried it in numberous ways but still can't get result. This is my current code: Don't care about singleton and logMessage, these just works.
class LogManager : public Singleton<LogManager>
{
public:
	inline friend void operator <<
	( ostream& o, ostream& o2 )
	{
		stringstream s;
		s << o2;
		LogManager::getSingleton().logMessage(s.str());
	}
};
But when I include LogManager << "Hello"; anywhere, GCC says "error: expected primary-expression before '<<' token". What am I doing bad, I have zero experience with << operators, but why the hell do I need two paramaters? I guess first one is for predessor like "first << LogManager << second" but I am not sure.

Share this post


Link to post
Share on other sites
Advertisement
Okay the way the IOstreams do it has little to do with operator overloadding itself, the trick is called cascading functions, you could do it either by:


struct LogManager {
// ....

// doesn't have to be a friend functions.
template < typename Tp >
friend LogManager& operator<<(LogManager& l, const Tp& t) {
//....
return l;
}
};


or:


struct LogManager {
// ....
template < typename Tp >
LogManager& operator<<(const Tp& t) {
//....
return *this;
}
};


cascading functions can be done for normal (member) functions aswell.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!