class logger {
....
};
logger& operator<<(logger& log, const std::string& str)
{
cout << "My Log: " << str << endl;
return log;
}
logger log;
log << "Lexicon Starting";
Works fine, but i would like to use a pointer to a class instance instead. i.e.
logger * log = new log();
log << "Lexicon Starting";
Is this possible? If so what is the syntax? Thanks
Edit: The compiler Error is
error: invalid operands of types 'logger*' and 'const char [17]' to binary 'operator<<'
You'd have to dereference the pointer to your logger object and obviously check if it's not 0. Something like this should do the job:
log && ((*log) << "Lexicon starting")
As a general aside, I would shy away from referencing objects like a logger (which you normally unconditionally expect to be present) via a pointer due to the uncertainty you get with a pointer, AKA is there an object or not?
Here is the way:
logger * log = new log();
(*log) << "Lexicon Starting";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With