64,646
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class TestBase
{
private:
int a;
public:
TestBase(){ a=0; }
~TestBase(){}
void print()
{
cout<<"TestBase print:"<<a<<endl;
}
};
class TestOne:public TestBase
{
private:
int a;
public:
TestOne(){ a=1; }
~TestOne(){}
void print()
{
cout<<"TestOne print"<<a<<endl;
}
};
int main(int argc, char* argv[])
{
TestOne* one = (TestOne*)new TestBase;
one->print();
return 0;
}
B* ptr=(B*)new A;
已经导致为定义行为了,后面的访问都不行。