C ++接口的工厂功能实现

时间:2018-07-04 12:13:04

标签: c++ shared-ptr biometrics make-shared facial-identification

上下文关联:

我正在研究一种面部识别算法,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()以便将引用的“托管指针返回到Interface对象”?

1 个答案:

答案 0 :(得分:1)

我认为您可以执行以下操作:

std::shared_ptr<Interface> Implementation::getImplementation() {
  return std::make_shared<Implementation>();
}

int main() {
    auto interface = Implementation::getImplementation();
}

并且interface的类型为std::shared_ptr<Interface>。您可以进一步传递它。