c++ 继承方式高内聚read write function操作

cpp 复制代码
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct BaseDevice
{
  BaseDevice(const std::string sType, const std::string sApplication) : strType(sType), strApplication(sApplication)
  {}

  virtual ~BaseDevice();


  std::string strType;
  std::string strApplication;
};
BaseDevice::~BaseDevice() {}

struct GetDeviceInfor : public BaseDevice
{
  GetDeviceInfor(const std::string sType, const std::string sApplication) : BaseDevice(sType, sApplication)
  {}

  virtual ~GetDeviceInfor();
};
GetDeviceInfor::~GetDeviceInfor()
{}

template<class T1>
struct SetDeviceInfor : public BaseDevice
{
  SetDeviceInfor(const std::string sType, const std::string sApplication, T1 iValues):BaseDevice(sType, sApplication), iValues(iValues)
  {}

  ~SetDeviceInfor() 
  {}

  T1 iValues;
};


int main(int argc, char** argv)
{
  std::vector<std::shared_ptr<BaseDevice>> vecDeviceInfor = {
  std::make_shared<GetDeviceInfor>("TemputerData", "FirstScreen"),
  std::make_shared<GetDeviceInfor>("RealValue", "MeasurmentMode"),
  std::make_shared<SetDeviceInfor<std::string>>("MachineValue", "MaintainerMode", "45")
  };

  for (auto pDeviceInfor : vecDeviceInfor)
  {
    if (pDeviceInfor)
    {
      std::shared_ptr<GetDeviceInfor> pGetDeviceTemoInfor = std::dynamic_pointer_cast<GetDeviceInfor>(pDeviceInfor);
      if (pGetDeviceTemoInfor != nullptr)
      {
        std::cout << pGetDeviceTemoInfor->strApplication << std::endl;
      }

      std::shared_ptr<SetDeviceInfor<std::string>> pSetDeviceTemoInfor = std::dynamic_pointer_cast<SetDeviceInfor<std::string>>(pDeviceInfor);
      if (pSetDeviceTemoInfor != nullptr)
      {
        std::cout << pSetDeviceTemoInfor->strApplication << std::endl;
        std::cout << pSetDeviceTemoInfor->iValues << std::endl;
      }
    }
    else
    {
      std::cout << "empty container!" << std::endl;
    }
  }
}
相关推荐
折哥的程序人生 · 物流技术专研4 分钟前
Java面试85题图解版 · 全系列总目录
java·开发语言·后端·面试·职场和发展
gf132111110 分钟前
飞书长连接_事件订阅(接收消息,审批任务状态变更)
开发语言·python·飞书
木易 士心13 分钟前
Java 跳出多层循环
java·开发语言·后端
kyle~15 分钟前
C++---段错误(SIGSEGV)
linux·运维·c++·机器人
乐观勇敢坚强的老彭17 分钟前
day515C++信奥循环嵌套强化03
开发语言·c++
杜子不疼.18 分钟前
【C++ AI 大模型接入 SDK】 - 环境搭建
开发语言·数据库·c++
怀旧,18 分钟前
【C++项目】负载均衡式在线OJ
开发语言·c++·负载均衡
yujunl25 分钟前
U9系统admin用户账号密码生成Do方法
开发语言
MaikieMaiky33 分钟前
C++ STL 系列(一):string 容器详解与示例
开发语言·c++
之歆33 分钟前
DAY_25 JavaScript 原型、原型链与值类型/引用类型 ── 深度全解(下)
开发语言·javascript·ecmascript