也许这是一个简单的问题,因为我对 C++ 还是个新手。我想使用某种工厂来封装我的应用程序中的日志记录。这个想法是只有工厂知道哪个具体类将处理函数调用。应用程序将始终调用基本日志记录类的抽象接口(interface)。
工厂方法应该是这样的:
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 ,但我仍然没有看到解决方案。
最佳答案
你想要的 std::unique_ptr
构造函数是 explicit
,因此你需要......好吧......明确说明它。尝试
return std::unique_ptr<AbstractLoggingClass>(new ConcreteLoggingClass(param));
关于c++ - 在工厂中返回 unique_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34397430/