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

这篇具有很好参考价值的文章主要介绍了c++ 继承方式高内聚read write function操作。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

代码示例1

#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;
    }
  }
}

结果如下

c++ 继承方式高内聚read write function操作,C++,c++,开发语言

代码示例2

派生类增加传入指定函数

#include <iostream>
#include <fstream>
#include <vector>
#include <functional>

using namespace std;

class StorageClass
{
public:
  StorageClass();
  ~StorageClass();
  void setData(int iData)
  {
    iOperateData = iData;
  }
  void setStringData(std::string sData)
  {
    std::cout << "setData:" << sData << std::endl;
    strData = sData;
  }

  int getData()
  {
    return iOperateData;
  }
  std::string getStringData()
  {
    return strData;
  }

private:
  int iOperateData = 1;
  std::string strData;
};

StorageClass::StorageClass()
{

}

StorageClass::~StorageClass()
{

}

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(std::function<int(std::unique_ptr<StorageClass>&)> cFunction, const std::string sType, const std::string sApplication) : BaseDevice(sType, sApplication), cFunction(cFunction)
  {}

  virtual ~GetDeviceInfor();
  std::function<int(std::unique_ptr<StorageClass>&)> cFunction;
};
GetDeviceInfor::~GetDeviceInfor()
{}

template<class T1>
struct SetDeviceInfor : public BaseDevice
{
  SetDeviceInfor(std::function<void(std::unique_ptr<StorageClass>&, T1) > cFunction, const std::string sType, const std::string sApplication, T1 iValues):
    BaseDevice(sType, sApplication), 
    iValues(iValues), 
    cFunction(cFunction)
  {}

  ~SetDeviceInfor() 
  {}

  T1 iValues;
  std::function<void(std::unique_ptr<StorageClass>&, T1) > cFunction;
};


int main(int argc, char** argv)
{
  std::vector<std::shared_ptr<BaseDevice>> vecDeviceInfor = {
  std::make_shared<GetDeviceInfor>(&StorageClass::getData, "TemputerData", "FirstScreen"),
  std::make_shared<GetDeviceInfor>(&StorageClass::getData, "RealValue", "MeasurmentMode"),
  std::make_shared<SetDeviceInfor<std::string>>(&StorageClass::setStringData, "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::unique_ptr<StorageClass> puniDevice = std::make_unique<StorageClass>();
        int rValue = pGetDeviceTemoInfor->cFunction(puniDevice);
        std::cout << "rValue:" << rValue << 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;

        std::unique_ptr<StorageClass> puniDevice = std::make_unique<StorageClass>();
        pSetDeviceTemoInfor->cFunction(puniDevice, "123");
        std::cout << "GetData:" << puniDevice->getStringData() << std::endl;
      }
    }
    else
    {
      std::cout << "empty container!" << std::endl;
    }
  }
}

输出结果如下

c++ 继承方式高内聚read write function操作,C++,c++,开发语言文章来源地址https://www.toymoban.com/news/detail-719929.html

到了这里,关于c++ 继承方式高内聚read write function操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • c++—继承、继承方式

    1. c++三大核心功能     (1)封装:提高代码的维护性,遇到问题可以准确定位;     (2)继承:提高代码的复用性,注意不是ctrl+c,ctrl+v,而是不做任何修改或操作源码就能实现代码的复用;     (3)多态:提高代码的扩展性; 2. 代码复用的两种方式     (1)组合(ha

    2024年02月06日
    浏览(27)
  • read/write和fread/fwrite介绍

    UNIX环境下的C 对二进制流文件的读写有两套班子:1) fopen,fread,fwrite ; 2) open, read, write 这里简单的介绍一下他们的区别。 1. fopen 系列是标准的C库函数;open系列是 POSIX 定义的,是UNIX系统里的system call。 也就是说,fopen系列更具有可移植性;而open系列只能用在 POSIX 的操作系

    2024年02月09日
    浏览(29)
  • Linux-open、read、write函数

    1、open函数 详细使用可以使用Linux命令:man 2 open flags参数 :(注意这里可以使用 |来添加多个参数),如: flags三个访问权限参数:( 注意这三个参数在flags中只能出现其中一个 ) O_RDONLY:只读          O_WRONLY:只写          O_RDWR:读写 flags其他参数: O_CREAT:若文件不

    2024年02月15日
    浏览(33)
  • IO学习系列之使用read和write复制文件内容

    read函数: 功能:从 文件fd 中读取 count个字节 ,存放进 指针buf ; 具体内容: write函数: 功能:把 指针buf 中的内容,写 count个字节 到 文件fd 中; 具体内容: 示例代码:

    2024年02月07日
    浏览(33)
  • Linux-0.11 文件系统read_write.c详解

    该模块实现了文件系统通用的读写的方法read/write/lseek。 根据文件类型的不同,在内部将调用不同的方法。如果是管道文件,则调用pipe.c中的读写方法,如果是字符设备,则会调用char_dev.c中的方法,如果是目录或者普通文件,将调用file_dev.c中的读写方法,如果是块设备文件,

    2024年02月06日
    浏览(33)
  • Linux0.11内核源码解析-read_write.c

    目录  sys_lseek read write read_write.c主要是实现文件系统调用read(),write()和lseek()三个功能 read和write函数分别是调用file_dev.c/pipe.c/block_dev.c/char_dev.c实现相对应的函数 lseek实现系统调用将对文件句柄对应文件结果体中的当前读写指针进行修改,对于读写指针不能移动的文件和管道文

    2024年02月13日
    浏览(32)
  • C#,入门教程(28)——文件夹(目录)、文件读(Read)与写(Write)的基础知识

    上一篇:   C#,入门教程(27)——应用程序(Application)的基础知识 https://blog.csdn.net/beijinghorn/article/details/125094837 C#知识比你的预期简单的多,但也远远超乎你的想象! 与 文件 相关的知识,不算多。 作为初学者,先学习 文本文件 的读写,就足够应付好几年了。 文件 自然是

    2024年01月23日
    浏览(50)
  • TCP Socket性能优化秘籍:掌握read、recv、readv、write、send、sendv的最佳实践

    💡一个热爱分享高性能服务器后台开发知识的博主,目标是通过理论与代码实践的结合,让世界上看似难以掌握的技术变得易于理解与掌握。技能涵盖了多个领域,包括C/C++、Linux、Nginx、MySQL、Redis、fastdfs、kafka、Docker、TCP/IP、协程、DPDK等。 👉 🎖️ CSDN实力新星,社区专家

    2024年02月13日
    浏览(30)
  • Android 13 WRITE_EXTERNAL_STORAGE , READ_EXTERNAL_STORAGE不弹出的问题

    在Android 13(API 33)之前,加入了如下代码 如果 compileSdk 设置 32 或者以下,那么就没什么问题。当设置33 或者以上,系统就会自动设置无权限。那在 33 及以上的怎么设置呢? 看下面的代码。 在xml中如下设置 动态获取权限的代码如下:

    2024年04月27日
    浏览(51)
  • spirngboot连接redis报错:READONLY You can‘t write against a read only replica.

    docker 部署的redis,springboot基本每天来连redis都报错: READONLY You can\\\'t write against a read only replica. 重启redis后,可以正常连接。 但是每天都重启redis,不现实,也很麻烦。 进入redis容器 进入redis工程化 查看当前角色 设置slave只读为no 问题解决。

    2024年02月11日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包