在 C++ 中扩展后将 header 绑定到实现
Bind header to implementation after extending in C++
来自 PHP 和 Java 的背景,我想用 C++ 做一些事情,这并不像我希望的那么容易。
假设我有一个 header 文件(我将其视为一个接口),其中包含 class Sensor
我有多种类型的传感器,但它们都具有基本功能,例如
- 初始化
- 阅读
- 写
但是,对于每个传感器,它们都以不同的方式实现。
在 PHP 中,我将能够使用 IoC 和依赖注入将特定实现绑定到接口。
如何使用每个传感器的具体实现来扩展 "interface"/header?
我想像这样的文件结构:
sensor.h
sensor1.cpp
sensor2.cpp
其中 .cpp
文件都是 Sensor
接口的不同实现。
我的编译器与 C++11 兼容,我更喜欢以 "new" 的方式做事。
通常,在 C++ 中,class 的声明及其实现位于两个不同的文件中:头文件 (.h) 通常仅包含声明,而 .cpp 文件包含实现。有关此分离的更多详细信息,请参阅此 post:Separating class code into a header and cpp file。
此外,为了使同一接口具有不同的实现,最简单的方法是使用继承。下面是一个使用它的简单示例。
#include <vector>
#include <iostream>
// This could be in SensorInterface.h
class SensorInterface
{
public:
virtual void initialize() = 0;
virtual void read() = 0;
virtual void write() = 0;
};
// This could be in SensorImplementation1.h (for definitions) and SensorImplementation1.cpp (for implementation)
class SensorImplementation1 : public SensorInterface
{
public:
virtual void initialize()
{
std::cout << "SensorImplementation1::initialize" << std::endl;
// Here implementation
}
virtual void read()
{
std::cout << "SensorImplementation1::read" << std::endl;
// Here implementation
}
virtual void write()
{
std::cout << "SensorImplementation1::write" << std::endl;
// Here implementation
}
};
// This could be in SensorImplementation1.h (for definitions) and SensorImplementation1.cpp (for implementation)
class SensorImplementation2 : public SensorInterface
{
public:
virtual void initialize()
{
std::cout << "SensorImplementation2::initialize" << std::endl;
// Here implementation
}
virtual void read()
{
std::cout << "SensorImplementation2::read" << std::endl;
// Here implementation
}
virtual void write()
{
std::cout << "SensorImplementation2::write" << std::endl;
// Here implementation
}
};
int main(int argc, char **argv)
{
std::vector<SensorInterface *> sensors;
SensorImplementation1 sensor1;
SensorImplementation2 sensor2;
sensors.push_back(&sensor1);
sensors.push_back(&sensor2);
for(unsigned int i = 0; i < sensors.size(); i++)
{
sensors.at(i)->initialize();
sensors.at(i)->read();
sensors.at(i)->write();
}
return 0;
}
来自 PHP 和 Java 的背景,我想用 C++ 做一些事情,这并不像我希望的那么容易。
假设我有一个 header 文件(我将其视为一个接口),其中包含 class Sensor
我有多种类型的传感器,但它们都具有基本功能,例如
- 初始化
- 阅读
- 写
但是,对于每个传感器,它们都以不同的方式实现。
在 PHP 中,我将能够使用 IoC 和依赖注入将特定实现绑定到接口。
如何使用每个传感器的具体实现来扩展 "interface"/header?
我想像这样的文件结构:
sensor.h
sensor1.cpp
sensor2.cpp
其中 .cpp
文件都是 Sensor
接口的不同实现。
我的编译器与 C++11 兼容,我更喜欢以 "new" 的方式做事。
通常,在 C++ 中,class 的声明及其实现位于两个不同的文件中:头文件 (.h) 通常仅包含声明,而 .cpp 文件包含实现。有关此分离的更多详细信息,请参阅此 post:Separating class code into a header and cpp file。
此外,为了使同一接口具有不同的实现,最简单的方法是使用继承。下面是一个使用它的简单示例。
#include <vector>
#include <iostream>
// This could be in SensorInterface.h
class SensorInterface
{
public:
virtual void initialize() = 0;
virtual void read() = 0;
virtual void write() = 0;
};
// This could be in SensorImplementation1.h (for definitions) and SensorImplementation1.cpp (for implementation)
class SensorImplementation1 : public SensorInterface
{
public:
virtual void initialize()
{
std::cout << "SensorImplementation1::initialize" << std::endl;
// Here implementation
}
virtual void read()
{
std::cout << "SensorImplementation1::read" << std::endl;
// Here implementation
}
virtual void write()
{
std::cout << "SensorImplementation1::write" << std::endl;
// Here implementation
}
};
// This could be in SensorImplementation1.h (for definitions) and SensorImplementation1.cpp (for implementation)
class SensorImplementation2 : public SensorInterface
{
public:
virtual void initialize()
{
std::cout << "SensorImplementation2::initialize" << std::endl;
// Here implementation
}
virtual void read()
{
std::cout << "SensorImplementation2::read" << std::endl;
// Here implementation
}
virtual void write()
{
std::cout << "SensorImplementation2::write" << std::endl;
// Here implementation
}
};
int main(int argc, char **argv)
{
std::vector<SensorInterface *> sensors;
SensorImplementation1 sensor1;
SensorImplementation2 sensor2;
sensors.push_back(&sensor1);
sensors.push_back(&sensor2);
for(unsigned int i = 0; i < sensors.size(); i++)
{
sensors.at(i)->initialize();
sensors.at(i)->read();
sensors.at(i)->write();
}
return 0;
}