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

```
Object `t` has type pointer-to-`Base` and `Base` does not define a `data` member, which is the reason why your code fails to compile. If you know for certain that `t` points to an actual object of type `Deriv` (as is the case with your posted code), you can cast it in order to be able to call `Deriv` members:

```
Deriv* d = static_cast<Deriv*>(t);
std::cout << d->data << std::endl;

```

If you didn't know that `t` points to an actual object of type `Deriv`, you would need to polymorphically downcast it via dynamic\_cast and check the return value:

```
Deriv* d = dynamic_cast<Deriv*>(t);
if (d != nullptr)
{
    std::cout << d->data << std::endl;
}

```

In any case, based on the code you posted, you don't really gain anything by declaring `t` as a `Base*` if you're just using it to access a data member defined only by `Deriv`.