(*m_log) << "message;
The parentheses are not necessary. Just do this: *m_log << "message";
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
You could do something like this
#include <fstream>
#include <iostream>
#include <string>
class MyClass
{
public:
std::ostream& foo(std::ostream& stream, std::string msg);
};
std::ostream& MyClass::foo(std::ostream& stream, std::string msg)
{
stream << msg << '\n';
return stream;
}
int main()
{
MyClass m;
m.foo(std::cout, "Hello");
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
How about you make a helper function to reduce work.
class Logger{
private:
ostream *stream;
public:
Logger(ostream& strm) { stream = &strm; }
void log(const std::string& msg){ _log(msg); }
private:
void _log(const std::string& msg){ (*stream) << msg << endl; }
};
And so now you just call _log(msg) instead.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608