【发布时间】:2017-12-05 13:24:53
【问题描述】:
我想知道是否有人可以解释我如何使用接口。 通常,接口应在层和架构对象之间的所有情况下使用(允许可测试性、清晰的结构/架构、团队独立工作......)
我还不明白的是,包含依赖项.. 我仍然需要实例化对象 Test 本身,因此需要将它直接包含在上面的层(arch)中。但我宁愿不知道那里发生了什么,只使用界面。
去这里的路是什么? 有人有一个具体的例子吗(例如:HAL::Timer 作为对象,HAL:IF_Timer 作为接口,中间件/应用程序是什么来创建这样的对象并使用它?
// =============== IF_Test.hpp =======================
#ifndef IF_Test_hpp
#define IF_Test_hpp
#include <stdio.h>
class I_Test
{
public:
I_Test() { };
virtual ~I_Test() { };
virtual std::string& toString() = 0;
};
#endif /* IF_Test_hpp */
// =============== Test.hpp =========================
#ifndef Test_hpp
#define Test_hpp
#include <stdio.h>
#include "IF_Test.hpp"
class Test : public I_Test
{
std::string myName;
public:
Test();
~Test();
std::string& toString();
};
#endif /* Test_hpp */
// =============== Test.cpp =========================
#include <iostream>
#include <cstdio>
#include <string>
#include "Test.hpp"
Test::Test()
: myName("PeterParkerIsBatman")
{
std::cout << "Test\n";
}
Test::~Test()
{
std::cout << "!Test\n";
}
std::string& Test::toString()
{
return myName;
};
// =============== main.cpp =========================
#include <iostream>
#include "IF_Test.hpp"
/** HERE i still need to include the
concrete class object, which id likte NOT to do
(Or do i want this and why?) */
#include "Test.hpp"
int main(int argc, const char * argv[])
{
I_Test * obj = new(Test);
obj->toString();
std::cout << "Hello, World!\n";
return 0;
}
【问题讨论】:
-
C++ 中没有接口
-
旁白 - I_TEST 需要一个虚拟析构函数
-
我知道,但是类中的纯虚成员函数作为接口的选项??
-
通常,接口的消费者不会实例化具体类型。
-
所以可以说我有一个到硬件基本定时器的接口(setDelay()、start()、attach()、stop()、...),然后是一个实现定时器的类。在这种情况下,接口几乎没用,因为我无论如何都需要包含 Timer 类??
标签: c++ class interface embedded