c++ - QDataStream operator << overloading for Abstract class -
i want overload << operator abstract class virtual operator, know how overload operator simple class , code below sample.
class normalclass { public: int firstfield() const; void setfirstfield(int firstfield); int secondfield() const; void setsecondfield(int secondfield); private: int m_firstfield; int m_secondfield; }; qdatastream &operator<<(qdatastream &out, const normalclass &obj) { out << obj.firstfield(); out << obj.secondfield(); return out; }
but lets have abstractbaseclass ,childclass below
class abstractbaseclass { public: abstractbaseclass() {} int basefirstfield() const; void setbasefirstfield(int basefirstfield); int basesecondfield() const; void setbasesecondfield(int basesecondfield); private : int m_basefirstfield; int m_basesecondfield; }; class childclass : public abstractbaseclass { public: childclass() {} int childfirstfield() const; void setchildfirstfield(int childfirstfield); private: int m_childfirstfield; }
now want overload << operator abstractbaseclass childclass should implement it.
this try
#ifndef abstractbaseclass_h #define abstractbaseclass_h #include <qdatastream> class abstractbaseclass { public: abstractbaseclass(); int basefirstfield() const; void setbasefirstfield(int basefirstfield); int basesecondfield() const; void setbasesecondfield(int basesecondfield); private : int m_basefirstfield; int m_basesecondfield; }; virtual qdatastream &operator<<(qdatastream &out, const abstractbaseclass &obj)=0; #endif // abstractbaseclass_h
compiler complains : virtual outside class declaration , have idea?
what have try , compiled , think of method , there better achievements.
this abstract class
.h
#ifndef abstractbaseclass_h #define abstractbaseclass_h #include <qdatastream> class abstractbaseclass { public: abstractbaseclass(); int basefirstfield() const; void setbasefirstfield(int basefirstfield); int basesecondfield() const; void setbasesecondfield(int basesecondfield); virtual qdatastream &serialize(qdatastream &stream)=0; private : int m_basefirstfield; int m_basesecondfield; }; qdatastream &operator<<(qdatastream &out, abstractbaseclass &obj); #endif // abstractbaseclass_h
.cpp
#include "abstractbaseclass.h" abstractbaseclass::abstractbaseclass() { m_basefirstfield = 0; m_basesecondfield = 0; } int abstractbaseclass::basefirstfield() const { return m_basefirstfield; } void abstractbaseclass::setbasefirstfield(int basefirstfield) { m_basefirstfield = basefirstfield; } int abstractbaseclass::basesecondfield() const { return m_basesecondfield; } void abstractbaseclass::setbasesecondfield(int basesecondfield) { m_basesecondfield = basesecondfield; } qdatastream &operator<<(qdatastream &out, abstractbaseclass &obj) { return obj.serialize(out); }
and child class
class childclass : public abstractbaseclass { public: childclass(){} int childfirstfield() const { return m_childfirstfield; } void setchildfirstfield(int childfirstfield) { m_childfirstfield = childfirstfield; } private: int m_childfirstfield; // abstractbaseclass interface public: qdatastream &serialize(qdatastream &stream) override { stream << basefirstfield(); stream << basesecondfield(); stream << childfirstfield(); return stream; } };
Comments
Post a Comment