CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2002
    Posts
    788

    operator << and >> overloading for derived classes

    Code:
    class  Var
    {
    public:
    	Var();
    	~Var();
    
    	
    private:
    	QMap<QString, QSharedPointer<Data>> _mapVars;
    
    };
    
    QDataStream &operator<<(QDataStream &stream, const QSharedPointer<Data> p_data);
    QDataStream &operator>>(QDataStream &stream, Data &p_data)
    I want to serialize _mapVars into a file. However, I have many other classes that are derived from Data,
    do i need to check for Data type inside the overloaded << functions like below in order to serialize ??? This doesn';t seem to be very correct ... What is the correct or more appropriate way of doing it?

    Code:
    QDataStream &operator<<(QDataStream &stream, const QSharedPointer<Data> p_data)
    {
     if(p_data->GetType == .....)
    {
    
    }
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: operator << and >> overloading for derived classes

    In the Data class, declare virtual member functions to perform serialisation. Overload these operators to perform serialisation for the Data class hierarchy by calling these member functions. For each Data subclass, override these virtual member functions to perform serialisation specific to the subclass.

    It is essentially the same approach as one might do for printing in a class hierarchy with standard I/O streams, except that it now involves QDataStream.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2002
    Posts
    788

    Re: operator << and >> overloading for derived classes

    i see, so it is something like below?

    Code:
    class Data
    {
    
    ....
    
    virtual QDataStreeam & Serialize(QDataStream &stream)
    {
    stream << x;
    
    return stream;
    }
    
    private:
    int x;
    
    };
    QDataStream &operator<<(QDataStream &stream, const QSharedPointer<Data> p_data)
    {
    
    return p_data->Serialize(stream);
    
    }
    I have an additional question, in my example, notice that i am using
    Code:
    QDataStream &operator>>(QDataStream &stream, Data & data)
    rather than
    Code:
    QDataStream &operator>>(QDataStream &stream, QSharedPointer<Data> data)
    I don't think i can deserialize a QDataStream to a map with pointers; like
    stream >> map; //where map is QMap<QString, QSharedPointer<Data>> ??
    Last edited by mce; July 29th, 2014 at 11:50 PM.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: operator << and >> overloading for derived classes

    I suggest something like this instead:
    Code:
    class Data
    {
    
        // ...
    
        virtual void Serialize(QDataStream& stream) const
        {
            stream << x;
        }
    private:
        int x;
    };
    
    QDataStream& operator<<(QDataStream& stream, const Data& data)
    {
        data.Serialize(stream);
        return stream;
    }
    
    QDataStream& operator<<(QDataStream& stream, QSharedPointer<Data> p_data)
    {
        // ...
    
        stream << *p_data;
    
        // ...
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2002
    Posts
    788

    Re: operator << and >> overloading for derived classes

    i guess you mean

    Code:
    QDataStream& operator<<(QDataStream& stream, QSharedPointer<Data> p_data)
    {
        // ...
    
        return stream << *p_data;
    
        // ...
    }
    
    or, should i have a const QSharedPointer<Data> instead?
    
    QDataStream& operator<<(QDataStream& stream, const QSharedPointer<Data> p_data)
    {
        // ...
    
        return stream << *p_data;
    
        // ...
    }
    Last edited by mce; July 30th, 2014 at 12:49 AM.

  6. #6
    Join Date
    Jul 2002
    Posts
    788

    Re: operator << and >> overloading for derived classes

    I am also stuck in deserialization now... how does the functions like like??
    i guess need to check the type now??

    Code:
    class Data
    {
    
        // ...
    
        virtual QSharedPointer<Data> DeSerialize(QDataStream& stream)
        {
            QSharedPointer<Data> data(new Data());
            stream >> data->x;
            return data;
        }
    private:
        int x;
    };
    
    
    QDataStream& operator>>(QDataStream& stream, const Data& data)
    {
        data.DeSerialize(stream);
        return stream;
    }
    
    QDataStream& operator>>QDataStream& stream, QSharedPointer<Data> p_data)
    {
        // ...
      
    I am blank here... what to do??
    
        // ...
    }

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: operator << and >> overloading for derived classes

    Quote Originally Posted by mce
    i guess you mean
    No, I meant what I wrote ("..."). Beyond that, it is up to you how you want to implement the function.

    Quote Originally Posted by mce
    or, should i have a const QSharedPointer<Data> instead?
    No real difference since it does not matter to the caller whether or not you modify p_data, so it is up to you.

    Quote Originally Posted by mce
    I am also stuck in deserialization now... how does the functions like like??
    i guess need to check the type now??
    It depends on how you serialise in the first place. One approach is to place some kind of header in the format such that you can parse this header to determine which subclass format it is, then use the appropriate subclass operator>> overload to deserialise.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Jul 2002
    Posts
    788

    Re: operator << and >> overloading for derived classes

    What do you think of the following implementation?

    Code:
    class Data
    {
    
        // ...
    
        virtual void DeSerialize(QDataStream& stream)
        {
            
            stream >> x;
        }
    private:
        int x;
    };
    QDataStream & operator>>(QDataStream &stream, const Data & p_data)
    {
    	
    }
    QDataStream &operator>>(QDataStream &stream, QSharedPointer<Data> p_data)
    {
           int type;
           p_stream << type;
           p_data = DataFactory->CreateData(type);
           p_data->Deserialize(stream);
           return p_stream;
    
    }
    
    main()
    {
                   QFile ff("c:\\mytest.txt");
    		ff.open(QIODevice::ReadOnly);
    
    		QDataStream instream(&ff);
                  QMap<QString, QSharedPointer<Data>> mymap;
                   instream >> mymap;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured