c++基类指针指向派生类对象能不能访问其内部变量
rt
#include <stdio.h>
#include "stdafx.h"
class A
{
public:
int x = 10;
};
class T:public A
{
public:
int y = 15;
};
T t;
int main()
{
A *p;
p = &t;
printf("%d ", p->y);
return 0;
}
你的p指针是基类A的,而A没有成员y的,所以你程序是错的
改
A *p;
为
T *p;
程序就可以运行了
热门标签: