I have a Logging class that is inherited by LoggingString, LoggingInt, etc. which each have a writeLog function that takes in the corresponding type:
class Logging {
public:
explicit Logging(const LoggingConfig& config);
...
}
class LoggingString : public Logging {
public:
using Logging::Logging;
writeLog(std::string str);
...
}
In the code where I use LoggingString, I want to be able to call a factory function like this:
std::unique_ptr<LoggingString> logger = LoggingString::CreateIfLoggingEnabled(LoggingConfig{...});
std::unique_ptr<LoggingInt> logger = LoggingInt::CreateIfLoggingEnabled(LoggingConfig{...});
Considering they're all using the same constructor derived from the base Logging class, is it possible to put this static Logging::CreateIfLoggingEnabled function in the base class and somehow have it inherited by the Logging derived classes, but returning a unique_ptr<LoggingDerivedClass> instead? Maybe something with templating like this:
class Logging {
public:
explicit Logging(const LoggingConfig& config);
template<typename T> static std::unique_ptr<T> CreateIfLoggingEnabled(const LoggingConfig& config) {
...
return std::unique_ptr<T>{};
}
...
}
std::unique_ptr<LoggingString> logger = LoggingString::CreateIfLoggingEnabled<LoggingString>(LoggingConfig{...});
Not sure if this would work, and besides it seems redundant to have to specify LoggingString twice. Any other options?