c++ - Can't access variable in template base class -
this question has answer here:
i want access protected variable in parent class, have following code , compiles fine:
class base { protected: int a; }; class child : protected base { public: int b; void foo(){ b = a; } }; int main() { child c; c.foo(); }
ok, want make templated. changed code following
template<typename t> class base { protected: int a; }; template <typename t> class child : protected base<t> { public: int b; void foo(){ b = a; } }; int main() { child<int> c; c.foo(); }
and got error:
test.cpp: in member function ‘void child<t>::foo()’: test.cpp:14:17: error: ‘a’ not declared in scope b = a; ^
is correct behavior? what's difference?
i use g++ 4.9.1
hehe, favourite c++ oddity!
this work:
void foo() { b = this->a; // ^^^^^^ }
unqualified lookup doesn't work here because base template. that's way is, , comes down highly technical details how c++ programs translated.
Comments
Post a Comment