结论:多态是用父类指针指向子类对象 和 父类指针++ 是两个不同的概念
案例:#include
using namespace std;
class Parent
{
public:
Parent(int a=0)
{
this->a = a;
}
virtual void print()
{
cout << “这是父类” << endl;
}
private:
int a;
};
class Child:public Parent
{
public:
Child(int b=0)
{
this->b = b;
}
virtual void print()
{
cout << “这是子类” << endl;
}
private:
int b;
};
void main()
{
Parent* p;
Child* c;
Child array[] = {Child(1),Child(2),Child(3)};
p = array;
c = array;
p->print();
c->print();
p++;
c++;
p->print();//此处父类指针步长与子类对象发生了冲突,程序奔溃了!
c->print();
system("pause");
}