C++ Cant accesss protected member variable without this pointer in template class

Viewed 31

Can someone please enlighten me on why this-> is required to access a parent class member variable in this scenario?

template <int S>
class A
{
protected:
    int x[S];
};

template <int S>
class B : public A<S>
{
    void Initialise()
    {
        this->x[0] = 0; // OK
        x[0] = 0;       // error: 'x' was not declared in this scope
    }
};

Thanks in advance...

EDIT:

This compiles OK:

template <int S>
class A
{
protected:
    int x[S];
};

class B : public A<5>
{
    void Initialise()
    {
        this->x[0] = 0; // OK
        x[0] = 0;       // OK
    }
};
0 Answers
Related