Question: I have the following program:

```
#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;
}

```

When I compile it, I'm getting the error:

```
x.cpp: In function ‘int main()’:
x.cpp:15:21: error: ‘class Base’ has no member named ‘data’

```

**How can I get access to data field (note that I don't want to use Deriv \*t = new Deriv(2))?**

Answer: 
Put this to base class:

```
virtual int get_data() const=0;

```

Then derived class will need to implement it - all data of the all derived classes will go through the interface:

```
class Base
{
    virtual int get_data() const = 0;
};

class Deriv: public Base
{
public:
    int data;
    Deriv(int data): data(data) {} 
    virtual int get_data() const
    { return data; }
};

```
You have declared your pointer to be of type `Base` and the compiler is correct, there is no member `data` in that class. You would need to cast the pointer back up to a `Deriv*` in order to access the member in this way. Why are you making the pointer of type to the base class anyway? This is only usually useful when you have a polymorphic class hierarchy.

You could consider using a virtual function to return the value of this member, but without knowing what your classes and code is trying to achieve it is hard to recommend one way or the other. Based on your small code sample, simply using 

```
Deriv *t = new Deriv(2);

```

instead, seems a better choice