Overloading the QDataStream << and>> operators for a user-defined type

前端 未结 2 531
慢半拍i
慢半拍i 2020-12-11 05:52

I have a an object I\'d like to be able to read and write to/from a QDataStream. The header is as follows:

class Compound
{
public:
    Compound(QString, QPi         


        
相关标签:
2条回答
  • 2020-12-11 06:01

    If you want to overload the "extract" operator >>, your signature must be:

    QDataStream & operator >> (QDataStream & in, MyClass & class);
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-11 06:02

    I think you've answered your own question! The stream operator

    QDataStream& operator<<( QDataStream&, const Compound& )
    

    will work fine. In the implementation you just use the existing stream operators on QDataStream to serialise the individual bits of your Compound. Some Qt classes define non-member QDataStream operators too. QString is one and so is QList so it looks like you're sorted!

    0 讨论(0)
提交回复
热议问题