关于基类指针赋值给子类的类型转换问题
#include<iostream>1、如何看待指针?(我的理解:它是一种数据类型,其实也是数据的一种;)
using namespace std;
class Base
{
public:
int a;
void Display(){cout<<"Base"<<endl;}
};
class Derive:public Base
{
public:
int b;
int c;
void Display(){cout<<"Derive"<<endl;}
};
int main()
{
Base *pA=new Base;
Derive *pB=new Derive;
cout<<sizeof(pA)<<endl;
cout<<sizeof(pB)<<endl;
Base BaseClsObject,*pBase;
pBase=&BaseClsObject;
Derive *pDer=(Derive*)pBase;
pDer->Display();
system("PAUSE");
return 0;
}
#include <iostream>
using namespace std;
class Base
{
public:
int a;
void Display(){cout<<"Base"<<endl;}
};
class Derive:public Base
{
public:
int b;
int c;
void Display(){cout<<"Derive"<<endl;}
};
int main()
{
Base BaseClsObject;
Derive *pDer=(Derive*)&BaseClsObject;
printf("%p %p", &BaseClsObject, pDer);
}
#include <iostream>
using namespace std;
class Base
{
public:
int a;
void Display(){cout<<"Base"<<endl;}
};
class Base2
{
public:
int d;
};
class Derive:public Base, public Base2
{
public:
int b;
int c;
void Display(){cout<<"Derive"<<endl;}
};
int main()
{
Derive d;
Base* pB = &d;
Base2* pB2 = &d;
printf("d = %p pB = %p pB2 = %p", &d, pB, pB2);
}
int c;
你说, 一个 Base2 的指针是不是该指向 int d; 的那个位置?