也许这是一个简单的问题,因为我还是C++的新手。我想用某种工厂来将日志记录封装在我的应用程序中。这个想法是只有工厂知道哪个具体类将处理函数调用。应用程序将始终调用基本日志记录类的抽象接口。返回工厂中的unique_ptr
工厂方法应该是这样的:
std::unique_ptr<AbstractLoggingClass> Factory::getDefaultLogger(const std::string& param){
return new ConcreteLoggingClass(param);
}
ConcreteLoggingClass
是AbstractLoggingClass
一个子类。
,但我得到了以下错误:
Error: could not convert '(operator new(64ul), (<statement>,
((ConcreteLoggingClass*)<anonymous>)))' from 'ConcreteLoggingClass*'
to 'std::unique_ptr<AbstractLoggingClass>'
我的问题是,我不知道如何实例ConcreteLoggingClass
并返回到unique_ptr
AbstractLoggingClass
我已经找到this post,但我还是不要没有看到解决方案。
为什么?请你解释一下,而不是默默无闻地冷静下来。 –
我仍然没有得到downvote。但是要感谢其他人提供有用的答案。 –