c++ - access static member of template parent class -


#include<iostream>  template<int n> class parent {     public:     static const unsigned int n_parent = 5 + n; };  template<int n> class son : public parent<n> {     public:     static void foo();     static const unsigned int n_son = 8 + n; };  template<int n> void son<n>::foo() {   std::cout << "n_parent = " << n_parent << std::endl;   std::cout << "n_son = " << n_son << std::endl; } 

this piece of code generate error

error: use of undeclared identifier 'n_parent'

i have specify template parameter explicitly:

template<int n> void son<dim>::foo() {   std::cout << "n_parent = " << son<n>::n_parent << std::endl;   std::cout << "n_son = " << n_son << std::endl; } 

why child template class can't deduce proper scope of inherited member implicitly?

the compiler not resolve inherited members template base classes, due fact may have specializations not define member, , in case whole parsing/name resolving business become quite difficult.

if member non-static, using this->n_parent "assures" compiler n_parent indeed member of base class. if member static, there no this pointer (as mention), choice qualify base class do.

related: derived template-class access base-class member-data


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -