访问派生类模板中的基类typedef
Accessing base class typedef in derived class template
我正试图从派生类模板访问基类中的typedef成员。在模板中,模板参数的名称和基类中typdef的名称相同。
#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";
在上面的代码T中,obj总是int类型。我怎么能强迫obj是template参数,而不是在基类中声明的typdef?
感谢
这对我有效:
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;
};
相关文章:
- std::具有相同基类的类的变体
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 基类中的函数名称解析
- C++初始化基类
- 如何通过派生类函数更改基类中的向量
- 如何定义一个纯抽象基类
- 如何使用基类指针引用派生类成员
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 模板基类typedef成员不可见
- 基类中的typedef在继承类中可见,而不是完全限定的
- 为什么在间接使用基类 typedef 时不断出现语法错误,而直接使用它就可以了?
- 在派生类中使用 typedef/using from 模板化基类
- 使用基类并从中重新定义typedef
- 使用CRTP时,在基类中使用非模板派生类的typedef
- 访问派生类模板中的基类typedef
- 为什么 std::atomic_{char,schar,etc.} typedef 允许 typedef 到 std::atomic 的基类,<T>而不是<T>仅原子?
- 可以将任何指针类型或基类传递到函数typedef中吗?
- 如何实现 typedef 基类的成员模板?
- 我可以使用类级别的typedef作为基类的模板参数吗?