代码示例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;
}
}
}
结果如下
代码示例2
派生类增加传入指定函数文章来源:https://www.toymoban.com/news/detail-719929.html
#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;
}
}
}
输出结果如下
文章来源地址https://www.toymoban.com/news/detail-719929.html
到了这里,关于c++ 继承方式高内聚read write function操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!