C++接口的工厂函数实现
C++ interface's factory function implementation
上下文化:
我正在研究一种面部识别算法,NIST是试图标准化所有可用算法之间的测试,测量和比较的组织。为了进行测试和比较,我需要实现它们的接口,该接口在 FRVT 项目中可用,更具体地说是在 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();
};
}
实现.h我正在开发的实现的相关代码:
#include "frvt11.h"
using namespace FRVT;
struct Implementation : public Interface {
ReturnStatus
initialize(const std::string &configDir) override;
static std::shared_ptr<Interface>
getImplementation();
};
实现.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>
型.您可以进一步传递它。
相关文章:
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 分段 排序函数实现中的错误
- 无法去函数实现 vim
- C++ 20 中的运算符 == 和 <=> 应该作为成员还是自由函数实现?
- 为什么在这种情况下不调用我的虚拟函数实现?
- 我能否通过将函数实现为类对象方法来避免使用互斥锁
- 嵌套的模板结构构造函数实现
- C++接口的工厂函数实现
- 链表中的递归长度函数实现
- 我可以期望某些 STL 函数实现是可自动矢量化的吗?
- 如何将深拷贝构造函数实现到链表中?
- 虚拟 CTOR 的克隆函数实现是否有问题
- 没有捕获列表的 lambda 通常作为普通函数实现吗?
- C++二叉树打印函数实现
- C++:默认构造函数实现
- C++派生类中的纯虚函数实现
- 决定放置函数实现的位置
- 强制实施纯虚函数实现,可能使用不同的参数类型
- 如何让成员函数实现依赖于类的模板参数?
- 我们如何将Ostream函数作为类的成员函数实现,而不是作为朋友函数,以便我可以用作虚拟函数