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 小时前
cout语句和cin语句
c++·cin·输入输出流·cout
Jiu-yuan4 小时前
C++文件操作
c++
2301_763472585 小时前
实时系统下的C++编程
开发语言·c++·算法
阿猿收手吧!5 小时前
【C++】深入理解C++ Atomic内存序:解决什么问题?怎么用?
开发语言·c++
小白学大数据5 小时前
Python爬虫实现无限滚动页面的自动点击与内容抓取
开发语言·爬虫·python·pandas
Andy Dennis5 小时前
一文漫谈设计模式之创建型模式(一)
java·开发语言·设计模式
小黄人软件5 小时前
【MFC】底层类显示消息到多个界面上。 MFC + 线程 + 回调 的标准模板 C++函数指针
c++·mfc
兩尛5 小时前
c++遍历容器(vector、list、set、map
开发语言·c++
£漫步 云端彡5 小时前
Golang学习历程【第十三篇 并发入门:goroutine + channel 基础】
开发语言·学习·golang
2301_790300965 小时前
C++与Docker集成开发
开发语言·c++·算法