c++ - C++接口的工厂函数实现
问题描述
语境化:
我正在研究一种面部识别算法,而 NIST 是试图标准化所有可用算法之间的测试、测量和比较的组织。为了进行测试和比较,我需要实现它们的接口,该接口在 FRVT Project中可用,更具体地说是在frvt11.h文件中。
frvt11.h这个问题的相关代码:
namespace FRVT {
//...A lot of code defining ReturnStatus, ReturnCode, etc.
/**
* @brief
* The interface to FRVT 1:1 implementation
*
* @details
* The submission software under test will implement this interface by
* sub-classing this class and implementing each method therein.
*/
class Interface {
public:
virtual ~Interface() {}
virtual ReturnStatus
initialize(const std::string &configDir) = 0;
/**
* @brief
* Factory method to return a managed pointer to the Interface object.
* @details
* This function is implemented by the submitted library and must return
* a managed pointer to the Interface object.
*
* @note
* A possible implementation might be:
* return (std::make_shared<Implementation>());
*/
static std::shared_ptr<Interface>
getImplementation();
};
}
implementation.h我正在开发的实现的相关代码:
#include "frvt11.h"
using namespace FRVT;
struct Implementation : public Interface {
ReturnStatus
initialize(const std::string &configDir) override;
static std::shared_ptr<Interface>
getImplementation();
};
implementation.cpp我正在开发的实现的相关代码:
#include "implementation.h"
using namespace FRVT;
ReturnStatus
Implementation::initialize(
const std::string &configDir) {
return ReturnStatus(ReturnCode::Success," - initialize");
}
std::shared_ptr<Interface>
Implementation::getImplementation() {
return (std::make_shared<Implementation>());
}
最后我的问题:
问题:如何实现 getImplementation() 以返回引用的“指向接口对象的托管指针”?
解决方案
我认为您可以执行以下操作:
std::shared_ptr<Interface> Implementation::getImplementation() {
return std::make_shared<Implementation>();
}
int main() {
auto interface = Implementation::getImplementation();
}
并且interface
将是类型std::shared_ptr<Interface>
。您可以进一步传递它。
推荐阅读
- ios - 当应用程序在 iOS12 和 Swift 4 中处于后台时,didReceiveRemoteNotification 不会触发
- pygame - Pygame音乐文件大小限制?
- c++ - 如何在VS2017中正确创建DLL项目?
- python - 如果不包含来自多子目录的 01.jpg,如何过滤文件名
- php - 从多个选定的术语中获取单个分类术语
- android - Gradle 同步失败 - 将 Android Studio 更新到 3.2 后
- python - Django 密码重置
- java - 从 War 创建登录 Tomcat
- .net-core - 为什么这里有 .net 框架选项
- office365 - 新团队 - 用户不是执行将此组迁移到团队的所有者