私は次のプログラムを持っています:
#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)を使用したくないことに注意してください)。