且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带指针的流运算符重载

更新时间:2023-11-10 10:42:10

让我解释一下.

例如:如果您保存一个int,Qt将从您那里获得一个int,它将以某种方式将该int存储到磁盘上(由操作员操作).另一方面,Qt将在创建一个int时,从磁盘(按操作员)加载其值,并将该int返回给您.

For example: If you save an int Qt will take an int from you and it will store that int in some way to disk (by operator). On loading on the other hand Qt will create an int, load its value from disk (by operator) and return that int to you.

对于您的指针,Qt会尝试以相同的方式进行操作:Qt将从您那里获取一个指针,并将其以某种方式存储到磁盘中(由您的操作员-访问对象对应于该指针).另一方面,在加载时Qt将创建一个未初始化的指针,并将尝试从磁盘(由您的操作员)加载其值并将其返回给您.但是,它需要做的是创建一个对象来初始化指针.

For your pointers Qt will try to do it the same way: Qt will take a pointer from you and it will store that pointer in some way to disk (by your operator - that accesses the object corresponding to that pointer). On loading on the other hand Qt will create a uninitialized pointer and will try to load its value from disk (by your operator) and to return it to you. However what it would need to do is to create an object to initialize the pointer.

您可能无法通过指针/引用存储/加载对象.始终仅存储/加载按值复制对象!尝试将操作转换为按值复制的对象,然后存储/加载该类型的对象.

You may not store/load objects by pointer/reference. Always store/load copy-by-value objects only! Try to convert your action into a copy-by-value object and store/load that kind of object.