c++ - 指向基类理解代码的指针列表
问题描述
我正在用 C++ 进行考试培训,但我不明白为什么代码只显示基类的打印功能(也在我调试代码之后)并且从不去打印派生类。你能用简单的方式解释一下为什么吗,我是否可以做一些问题,以便仅通过编辑几行代码来显示派生类的打印功能。假设程序生成的数字是:1 67 0 69 0 0 58 78。
class A
{
int a;
public:
A(int x):a(x){}
void print() { cout << "a=" << a << ' '; }
};
class B:public A
{
int b;
public:
B(int x,int y) :A(x),b(y) {}
void print() { A::print(); cout << "b=" << b << ' '; }
};
int main(){
list<A*> lst;
for (int i = 0; i < 3; i++)
if(rand()%2)
lst.push_back(new A(rand() % 100));
else
lst.push_back(new B(rand() % 100,rand()%100));
for (list<A*>::iterator it = lst.begin(); it != lst.end(); it++)
(*it)->print();
}
解决方案
您需要声明print()
virtual 才能使多态性起作用。
这是您建议的格式改进示例,
#include <iostream>
#include <list>
class A{
int a;
public:
A(int x):a(x){}
virtual void print() { std::cout << "a=" << a << ' '; }
};
class B:public A{
int b;
public:
B(int x,int y) :A(x),b(y) {}
void print() override { A::print(); std::cout << "b=" << b << ' '; }
};
int main(){
std::list<A*> lst;
for (int i = 0; i < 30; i++) {
if(rand()%2) {
lst.push_back(new A(rand() % 100));
} else {
lst.push_back(new B(rand() % 100,rand()%100));
}
}
for (std::list<A*>::iterator it = lst.begin(); it != lst.end(); it++) {
(*it)->print();
}
}
我增加了打印元素的数量,因为在这些设置中我没有得到一个b
. 这说:
srand
如果您不想一遍又一遍地获得相同的数字,则需要为代码播种(并且每个程序只需要播种一次)。- 如果可能,您应该使用
new random number
从 C++11 开始提供的生成功能。 override
是可选的,但非常鼓励,它清楚地表明该功能是虚拟的并重新实现。- 最重要的是,您需要释放您分配的内存
new
(使用delete
),或者更好的是,使用@TedLyngmo 答案中的方法(智能指针)
推荐阅读
- sql - 保留 SQLite 中表达式之前的子字符串
- linux - 每秒将“perf stat”输出保存到文件中
- python - sklearn Logistic回归中的C参数是什么?
- django - ElasticSearch、FarmHaystack、Django 连接被拒绝
- python-3.x - 我什么时候使用python装饰器函数@cache
- django - 如何准确过滤列表 JSONField?
- javascript - 按他们的孩子在猫鼬中排序文件
- react-native - React Native Paper Checkbox Item 在复选框之后放置标签
- ubuntu - 请帮我解决这些我正在安装我的 sql 突然错误来了
- vue.js - Vue 组件仅在鼠标悬停后更新