gpt4 book ai didi

c++ - QDataStream operator << 抽象类的重载

转载 作者:行者123 更新时间:2023-11-30 03:28:54 28 4
gpt4 key购买 nike

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

  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

编译器报错:virtual outside class declaration,你知道吗?

最佳答案

我试过编译成功了,你觉得这个方法怎么样,有没有更好的效果。

这是抽象类

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

关于c++ - QDataStream operator << 抽象类的重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46211228/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com