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;
    }
  }
}
相关推荐
摇滚侠1 分钟前
JavaWeb 全套教程 Listener 112-113
java·开发语言·servlet·tomcat·intellij-idea
小欣加油11 分钟前
leetcode2574 左右元素和的差值
数据结构·c++·算法·leetcode·职场和发展
hixiong12316 分钟前
C# Tokenizers.DotNet测试工具
开发语言·人工智能·llm
曹牧19 分钟前
Java:Deprecated 是
java·开发语言
weixin_4617694030 分钟前
通过数组和队列构造二叉树方法(用于算法测试),C++ vector不能直接使用null
数据结构·c++·算法·vector·nullptr·null
caimouse37 分钟前
Reactos 第 4 章 对象管理 — 4.1 对象与对象目录
服务器·c语言·开发语言·windows·架构
千寻girling41 分钟前
一周没跑步了 ,今日跑步 5KM , 哑铃+健身 20min , 俯卧撑 30 个 ;
数据结构·c++·python·算法·leetcode·职场和发展·线性回归
坚果派·白晓明43 分钟前
鸿蒙PC三方库使用:使用 AtomCode + Skills 自动完成鸿蒙化三方库spdlog集成
c++·华为·ai编程·harmonyos·skills·atomcode·c/c++三方库
半兽先生1 小时前
flv.js解决其中一个监控断线导致其他的监控播放阻塞
开发语言·javascript·ecmascript
玖玥拾1 小时前
C/C++ 基础笔记(九)联合、枚举及文件操作
c语言·c++·文件操作·枚举·联合