关于C++数据封装的问题,请教高手
#include<iostream.h>
class Base
{
private:
int a;
char b;
public:
Base()
{
a=1;
b='b';
}
virtual void show()
{
cout<<"In Base class: a="<<a<<endl<<endl;
cout<<"In Base class: b="<<b<<endl<<endl;
}
};
main()
{
int* x;
char* y;
Base b1;
//不用对象的接口,直接读出私有对象的值
x=reinterpret_cast<int*> (&b1); //第一个地址是virtual table的地址
x++; //跳到int a 的地址
cout<<*x<<endl; //输出对象中a的值
x++; //跳到char b的地址面,仍旧是int型的指针
y=reinterpret_cast<char*> (x); //转换成char的指针
cout<<*y<<endl; //输出对象中b的值
//不用对象的接口,直接改变私有对象的值
x--;
*x=100;
*y='z';
b1.show();
}