赞
踩
“typename”是一个C++程序设计语言中的关键字。当用于泛型编程时是另一术语”class”的同义词。这个关键字用于指出模板声明(或定义)中的非独立名称(dependent names)是类型名,而非变量名。
(1)作为class的同义词
这是一项C++编程语言的泛型编程(或模板编程)的功能,typename关键字用于引入一个模板参数,例如:
template <typename T>
const T& max(const T& x, const T& y)
{
return x > y ? x : y;
}
(2)使用关键字typename标识嵌套从属类型名称, 但不需要在基类列表和成员初始化列表内使用。
从属名称(dependent names): 模板(template)内出现的名称, 相依于某个模板(template)参数, 如T t;
嵌套从属名称(nested dependent names):从属名称在class内呈嵌套装, 如T::const_iterator ci;
非从属名称(non-dependent names): 不依赖任何templat
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。