带指针的流运算符重载 [英] Stream Operator Overload with pointers

查看:172
本文介绍了带指针的流运算符重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用QSettings存储指向自定义类的指针的列表,但是当我尝试从QSettings读回时出现内存错误.我已完成以下操作:

I am using QSettings to store a list of pointers to a custom class, but I get a memroy error when I try to read back from the QSettings. I have done the following:

  • 将我的自定义类声明为元类型:

  • Declare my custom class as metatype:

  Q_DECLARE_METATYPE(QCustomAction*)

  Q_DECLARE_METATYPE(QList<QCustomAction*>)

  • 将自定义类注册为元类型及其运算符:

  • Register custom class as metatype and its operator:

     qRegisterMetaType<QCustomAction*>("QCustomAction*"); 
     qRegisterMetaTypeStreamOperators<QCustomAction*>("QCustomAction*"); 
     qRegisterMetaType<QList<QCustomAction*>>("QList<QCustomAction*>");              
     qRegisterMetaTypeStreamOperators<QList<QCustomAction*>>("QList<QCustomAction*>");
    

  • 重载流运算符:

  • Overload Stream Operator:

     QDataStream &operator<<(QDataStream &out, QCustomAction* const  obj)
     {
     out << obj->m_actionName << obj->m_lastIndex;
     return out;
     }
     QDataStream &operator>>(QDataStream &in, QCustomAction* obj)
     {
     in >> obj->m_actionName >> obj->m_lastIndex;
     return in;    
     }
    

  • 在那之后,我的代码编译没有错误,我可以通过调用以下命令保存列表:

    After that, my code compiles without errors and I can save the list by calling:

         myQSettings.setValue("ActionsList",QVariant::fromValue<QList<QCustomAction*>>(someList)); 
    

    我将值读取为:

         someList =  myQSettings.value("ActionsList").value<QList<QCustomAction*>>();
    

    读取值时出现内存错误.我的猜测是,重载运算符存在一些问题.谁能给我一个我做错了什么的线索?

    I get a memory error when I read the value. My guess is that there are some problems witht the overloaded operators. Can anybody give me a clue of what I am doing wrong?

    推荐答案

    让我解释一下.

    例如:如果您保存一个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.

    这篇关于带指针的流运算符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆