我有以下程序:
#include <iostream>
class Base {};
class Deriv : public Base
{
public:
int data;
Deriv(int data): data(data) {}
};
int main()
{
Base *t = new Deriv(2);
std::cout << t->data << std::endl;
}
当我编译它时,我得到了错误:
x.cpp: In function ‘int main()’:
x.cpp:15:21: error: ‘class Base’ has no member named ‘data’
如何访问数据字段(请注意,我不想使用 Deriv *t = new Deriv(2))?