I want to overload << operator for abstract class as a virtual operator, I know how to overload the operator for simple class and code below is a 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 now let's say we have AbstractBaseClass, ChildClass like 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 I want to overload << operator for AbstractBaseClass which ChildClass should implement it.
This is my 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
the compiler complains: virtual outside class declaration, do you have any idea?
What I have try and compiled successfully , what do you think of this method and is there any better achievements.
This is abstract class
.h
.cpp
and child class