I am having an issue with inheritance. I created this example to show more or less my issue. The thing is that if I publicly derive from class that publicly derives from a class then I must have access to protected members in the original class all the way. However this doesn't seem to be the case when I'm using templates.
In fact, the example below complains on the line where 'n++;' saying that 'n' was not declared in the scope. However, if I do it without the templates. The code compiles just fine. What is going on?
#include<iostream>
template<typename T>
class base{
protected:
T n;
public:
T getn();
base();
};
template<typename T>
T base<T>::getn(){
return n;
}
template<typename T>
base<T>::base(){
n = 8;
}
template<typename T>
class daddy: public base<T>{
protected:
public:
};
template<typename T>
class granny: public daddy<T>{
protected:
public:
T plusone();
};
template<typename T>
T granny<T>::plusone(){
//this->n = this->n + 1;
n++;
return n;
}
int main(){
granny<int> oldmommy;
int su = oldmommy.getn();
std::cout << su << std::endl;
su = oldmommy.plusone();
std::cout << "plusone" << su << std::endl;
return 0;
}
Btw. Tell me if I should post the code with no templates to compare..