c++ - 如何从文件中加载 const 变量的内容?
问题描述
保存和检索具有 const成员变量的对象的内容(到/从磁盘文件)的方法是什么?
或者更具体地说, const 成员要求在对象创建时进行初始化。因此,内容的检索必须发生在初始化程序之前(在构造函数的 { } 之前)。如果我们不介意封装,我们可以检索并创建带有参数的对象。如何通过保持数据隐藏来做所有事情?
编译器:C++ 14 甚至更高版本。
对象的实例化,填充内容并为下一个上下文存储。
{ //CODE BLOCK 1 : making of content and saving to a diskfile
Abcd abcd(65535,256,25);
//some operations on abcd
//save to disk
QFile abcdFile("abcd.lion");
abcdFile.open(QFile::WriteOnly);
abcd.serialize(abcdFile);
abcdFile.close();
}
从文件中获取后使用相同的对象。
{ //CODE BLOCK 2 : loading from file and continue in another context
QFile abcdFile("abcd.lion");
abcdFile.open(QFile::ReadOnly);
Abcd abcdNew(abcdFile);
abcdFile.close();
if(!abcdNew.isHealthy())
printf("abcd from hdd is NOT Healthy :(\n");
else
{
//doTheJob(abcdNew);
}
}
班上。
#include <QFile>
class Abcd
{
const bool _healthy;//true if properly initialized
//IMPORTANT: _healthy has to be the first member in the class.
//this is to execute its initializer list first
protected:
const long _rX;
const long _rY;
long _count;
public:
Abcd(const long refX,
const long refY,
const long count) :
_healthy(true),
_rX(refX), _rY(refY),
_count(count)
{
}
Abcd(QFile &src) :
_healthy(deserialize(src)),
//Hack. Actually the initialization happened by this statement.
//just keeping the below statements for the sake of syntactical correctness. :(
_rX(_rX), _rY(_rY)
//,_count(count)
{
}
virtual
~Abcd()
{
}
inline
bool isHealthy()
{
return _healthy;
}
bool serialize(QFile &dest)
{
if(dest.write((char *)&_rY,sizeof(_rY))!=sizeof(_rY)) return false;
if(dest.write((char *)&_rX,sizeof(_rX))!=sizeof(_rX)) return false;
if(dest.write((char *)&_count,sizeof(_count))!=sizeof(_count)) return false;
return true;
}
private:
bool deserialize(QFile &src)
{
if(src.read((char *)&_rY,sizeof(_rY))!=sizeof(_rY)) return false;
if(src.read((char *)&_rX,sizeof(_rX))!=sizeof(_rX)) return false;
if(src.read((char *)&_count,sizeof(_count))!=sizeof(_count)) return false;
return true;
}
};
请提出更好的方法。为此,我在类的声明中引入了一个“健康”状态成员作为第一个成员。同样在反序列化中,我通过将 const 变量转换为 char * 指针来愚弄编译器。
解决方案
我的建议是使用static
类的成员函数从磁盘检索文件的内容,并在成功检索内容后构造一个对象。
代替:
Abcd(QFile &src) :
利用
static Abcd deserialize(QFile& src);
并将其实现为:
Abcd Abcd::deserialize(QFile& src)
{
long rX;
long rY;
long count;
if(src.read((char *)&rY, sizeof(rY)) != sizeof(rY)) throw false;
if(src.read((char *)&rX, sizeof(rX)) != sizeof(rX)) throw false;
if(src.read((char *)&count, sizeof(count)) != sizeof(count)) throw false;
return Abcd(rX, rY, count):
}
PS奇怪的是你_rY
先保存再保存_rX
。没有错,只是很奇怪。
推荐阅读
- python - 在 Pyspark 中动态读取 Hive 表分区文件
- sorting - Altair Color 覆盖/删除排序顺序
- html - shadowBlur(html画布)在js循环中不起作用
- python - Pandas:如何根据另一列替换列中的 Nan 值?
- python - 多个 .CSV 文件的高效快速合并
- spring-data-redis - 带有生菜的 RedisTemplate 似乎在每个请求上都获得了新的连接
- netlogo - 选择所有可能的配对之一并删除未选择的配对
- mysql - 编写查询,查找租借科幻电影 5 次以上的客户的全名。按字母顺序排列这些名称
- excel - 将个人的所有值从“是”更改为“否”
- javascript - React.js:获取“无法在'Node'上执行'removeChild':要删除的节点不是该节点的子节点。”