1

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

  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;

}

现在我想为应该实现它的 AbstractBaseClass 重载 << 运算符。

这是我的尝试

#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

编译器抱怨:虚拟外部类声明,你知道吗?

4

1 回答 1

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;
    }
};
于 2017-09-14T05:32:14.947 回答