首页 > 解决方案 > 指向基类理解代码的指针列表

问题描述

我正在用 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();
}

标签: c++listobjectinheritanceparent

解决方案


您需要声明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 答案中的方法(智能指针)

推荐阅读