64,647
社区成员
发帖
与我相关
我的任务
分享
class CA
{
int a;
public :
CA(int n):a(n){}
void showa()
{
cout<<"This is CA::showa() The value a = "<<a<<endl;
}
};
class CB
{
double b;
public :
CB(double n):b(n){}
void showb()
{
cout<<"This is CB::showb() The value b = "<<b<<endl;
}
};
void main()
{
CA a(3);
CB b(5.0);
CA *pA = &a;
CB *pB = &b;
pA->showa();
pB->showb();
pB = (CB*)pA;
pA->showa();
pB->showb();
system("pause");
}
#include<iostream>
#include<windows.h>
using namespace std;
class A
{
public:
void fun1(){};
};
class B:public A
{
public:
int j;
void fun2()
{
cout<<j<<endl;
cout<<"fun2"<<endl;
}
};
class C:public A
{
public:
int i;
void fun3()
{
cout<<i<<endl;
cout<<"fun3"<<endl;
}
};
int main()
{
A *a = new B;
((B*)a)->j=1000;
((C*)a)->fun3();
int i=99;
((C*)&i)->fun3();//静态绑定this+偏移访问内存即是i对应4个字节里内存
((B*)&i)->fun2();
//((B*)0)->fun2();this+偏移访问内存是非法内存
}
class CA
{
int a;
public :
CA(int n):a(n){}
void showa()
{
cout<<"This is CA::showa() The value a = "<<a<<endl;
}
};
class CA
{
int a;
public :
CA(int n):a(n){}
void showa()
{
cout<<"This is CA::showa() The value a = "<<a<<endl;
}
};
class CB
{
int b;
public :
CB(int n):b(n){}
void showb()
{
cout<<"This is CB::showb() The value b = "<<b<<endl;
}
};
void main()
{
CA a(3);
CB b(5);
CA *pA = &a;
CB *pB = &b;
pA->showa();
pB->showb();
pB = (CB*)pA;
pA->showa();
pB->showb();
}
class CA
{
public :
void showa()
{
cout<<"This is CA::showa()"<<endl;
}
};
class CB
{
public :
void showb()
{
cout<<"This is CB::showb()"<<endl;
}
};
void main()
{
CA a;
CB b;
CA *pA = &a;
CB *pB = &b;
pA->showa();
pB->showb();
pB = (CB*)pA;
pA->showa();
pB->showb();
}