QDataStream运算符<<重载Abstract类

时间:2017-09-14 05:32:15

标签: c++ qt operator-overloading abstract-class qdatastream

我想为抽象类重载<<< 运算符作为虚拟运算符,我知道如何为简单类重载运算符,下面的代码是一个示例

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

但现在让我们说我们有AbstractBaseClass,ChildClass如下

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;

}

现在我想重载&lt;&lt;用于AbstractBaseClass的运算符,ChildClass应该实现它。

这是我的尝试

#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

编译器抱怨:虚拟外部类声明,你有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我已经尝试并成功编译了什么,您如何看待这种方法并且有更好的成就。

这是抽象类

·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);
}

和儿童班

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