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
#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;
}
};