小白求助 关于父类指针和子类指针的步长问题
#include "iostream"using std::endl;
using std::cout;
using std::cin;
class Parent
{
private:
int a;
public:
Parent(int a = 0)
{
}
public:
virtual void print()
{
cout << "我是父亲\n";
}
};
class Child :public Parent
{
private:
//int b;
public:
Child(int b = 0) :Parent(11)
{
}
public:
virtual void print()
{
cout << "我是儿子\n";
}
};
void main()
{
Child cArr[] = { Child(11),Child(22),Child(33) };//这cArr是三个匿名对象,cArrcArrcArr 依次等于他们
Parent* pP = NULL;
Child* pC = NULL;
pP = cArr;
pC = cArr;
pP->print();//pP = cArr+0; pP++ = cArr+1;pP++ = cArr+2 那pP->print()是什么意思呢?父类指针pP指向子类对象 然后打印子类成员函数?
pC->print();
pP++;
pC++;
pP->print();
pC->print();
pP++;
pC++;
pP->print();
pC->print();
system("pause");
} 顶
页:
[1]