#include <iostream>
#include <list>
using namespace std;
class A{
public:
A(): data_(0){}
virtual void print(){
cout << "no" << endl;
}
protected:
int data_;
};
class B : public A{
public:
B(): A(){}
void print() override{
cout << data_ << endl;
}
};
list<A> my;
int main()
{
B* b = new B();
b->print();
my.push_back(*b);
for (list<A>::iterator i = my.begin(); i != my.end(); i++){
i->print();
}
}
The result of b->print
is 0
, but why is the result of i->print()
no
?
I want to make 0
come out when I use STL lists and polymorphism. What should I do?