Having a singleton Logger
class, I feel like writing Logger::GetInstance()
everytime a print method is called is ugly. The only solution I can think of is #define
. Is there anything better, or is this macro justified in this situation?
#include <iostream>
#include <fstream>
class Logger
{
public:
static Logger& GetInstance();
~Logger();
template <typename T>
void Print(const T &t);
void SetNewline(bool b);
void SetLogging(bool b);
private:
Logger();
Logger(const Logger&);
void operator=(const Logger&);
bool newline;
bool log;
std::ofstream file;
};
int main()
{
#define logger Logger::GetInstance()
logger.Print("Hello");
//Logger::GetInstance().Print("Hello");
}
note, since you're apparently defining a macro "locally", that macros do not respect scopes.
why not just define a function instead of a macro:
inline
auto logger() -> Logger& { return Logger::GetInstance(); }
then you can write just
logger().Print( "Hello" );
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