c++ - 为什么当我在超类中打印并在派生类中扩展此函数时,它在 C++ 中不显示特定属性?
问题描述
我有这个枚举类:
class Artifact{
public:
enum class artifactType{vase,jewelry,partialRelic,unknown};
...
在这堂课中,我制作了这个打印功能:
void print(std::ostream& os){
os<<"discoveryDate_:"<<discoveryDate_<<"-"<<"estimatedDateOfOrigin_:"<<estimatedDateOfOrigin_<<"-"<<"Enum that i dont know how to print:"<<"-----";
}
首先,我不知道如何打印这个枚举,其次,我有 2 个继承这个类的派生类:
class GreekArtifact : public Artifact{
private:
std::string nameOfGod_; //specific attribute
}
这个打印不工作
void print(std::ostream& os) {
Artifact::print(os);
os <<" God:"<<nameOfGod_;
}
我没有错误,但它只是没有显示 nameOfGod,因为在 main 中我创建了一个工件列表,当我打印时它显示 Artifact 类打印中的内容,而不是特定属性。我重载了运算符 <<
std::ostream& operator<<(std::ostream& os,const std::list<Artifact>& b){
for(auto it : b){
it.print(os);
os<<"\n";
}
return os;
}
解决方案
推荐阅读
- assembly - 在线汇编中操作寄存器需要帮助(STM32F103“BluePill”)
- javascript - 在 JavaScript 中使用 Promise 实现快速失败的设计
- java - 如何从在线而不是资产文件夹中读取 Json
- gremlin - AWS Neptune 设置基数单值?
- spring - IntelliJ 中的 SpringBoot。无法找到到所请求目标的有效认证路径
- javascript - 电影类型的页面转换?CSS
- postgresql - 如何在 EF Core 和 Postgresql 中使用日、月、年三个字段过滤日期时间
- wordpress - Q 我的网站真的很慢。我将 wordpress 与 elementor 一起使用
- java - 单选按钮未按预期显示
- swift - 'Int' 不能转换为 'CGFloat' Swift UI