A simple question about C ++ template inheritance

template <class T>
class baseclass{
protected:
    T data;
public:
    baseclass(){};
    void setData(T d);
};

template<class T>
void baseclass<T>::setT(T d){
    data = d;
}

My base class shown above, one protected member variable, one setter.

template <class T>
class aclass : public baseclass<T>
{
    public:
        aclass(T d);
};

template<class T>
aclass<T>::aclass(T d){
     setData(d); <---WORKS
     data = d;   <---DOESN'T WORK
}

Now this is my subclass of the first. For some reason, accessing a protected member variable does not work directly, although I believe it should be. However, access to the setter is working fine. I am noob with C ++, I am sure that I am missing something obvious.

+5
source share
1 answer

, , - , ++ . , , , ( ), , , . , .

,

template<class T>
aclass<T>::aclass(T d){
     setData(d);
     this->data = d;
}

, , data - aclass<T>, , .

, . , . , :

template<class T>
aclass<T>::aclass(T d){
     this->setData(d);
     this->data = d;
}

using, , aclass setData . :

template <class T>
class aclass : public baseclass<T>
{
    public:
        aclass(T d);

        using baseclass<T>::setData;
};

this-> , , setData , .

, !

+8

All Articles