Access to the base class typedef in the template of the derived class

I am trying to access a typedef element in a base class from a template of a derived class. In a template, the name of the template parameter matches the name typdef in the base class.

#include <iostream> using namespace std; class no { public : typedef int T; }; template<typename T> class no1 : public no { public : T obj; }; int main() { // your code goes here no1<string> o ; o.obj = "1"; return 0; } 14:24: error: invalid conversion from 'const char*' to 'no::T {aka int}' [-fpermissive] no1<string> o ; o.obj = "1"; 

In the above code, T obj is always of type int. How can I get obj to be from a template parameter, not a typdef declared in a base class?

thanks

+6
source share
1 answer

this works for me:

 template<typename T> class no1; template<typename T> struct underlying_type_of; template<typename T> struct underlying_type_of<no1<T> > { typedef T type; }; template<typename T> class no1 : public no { public : typename underlying_type_of<no1>::type obj; }; 
0
source

All Articles