【发布时间】:2019-07-16 03:27:02
【问题描述】:
具有 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 * 指针来愚弄编译器。
【问题讨论】:
-
想了一个结论。我有两个解决方案。 1. deserialize() 作为使用 RVO 和异常错误报告的静态函数。 2. 每种媒体(如磁盘文件、云等)的单独保存/检索对象。我为我的目的使用了第一个。由于在我的特定编译器的调试版本中未启用 RVO,因此创建了一个复制构造函数。
标签: c++ constructor c++14 deserialization constants