关于class和typename的一些补充

在这里插入图片描述

在大多数情况下class和typename是没有区别的,只不过在涉及嵌套从属类型会涉及到两者的差别,具体情况是什么,后文讲解

嵌套从属类型

我们假设有一个场景,我们需要设计一个Print打印函数,这个函数需要用于各种容器,那么首先这是泛型编程,Print需要是函数模板,我们思考到,所有容器都能够通过迭代器遍历,所以我们需要使用这些容器中的迭代器进行迭代打印:

template<class Container>
void Print(const Container& v)
{
	Container::const_iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << ' ';
		it++;
	}
	cout << endl;
}

于是就有上述代码,但是我们会发现,这段代码会报错

其原因在于,对于这种模板参数Container来说,我们通过域作用限定符:: 去访问到const_iterator,const_iterator可能是一个静态成员变量,也可能是typedef的内嵌类型或者是类部类类型,而编译器是默认不做任何修饰时将const_iterator解析为静态成员变量,所以此时,就需要用typename去修饰Container::const_iterator,让编译器认为它是一个类型,而非变量

template<class Container>
void Print(const Container& v)
{
	typename Container::const_iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << ' ';
		it++;
	}
	cout << endl;
}

这样就就能够正常的运行了,上面的嵌套从属类型是体现在,使用了从属于Container模板类中的类型const_iterator

除了上面这种场景,还有如下场景

template<class T>
void Print(const vector<T>& v)
{
	typename vector<T>::const_iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << ' ';
		it++;
	}
	cout << endl;
}

也就是说嵌套从属类型中有模板类型,就需要用typename进行修饰

那么关于我们就是想要用模板调用容器的静态成员变量的例子如下:

class AA
{
public:
	AA(){}
	
	static int _st;// 静态成员变量
};

template<class Container>
class BB
{
public:
	void fun(int st)
	{
		Container::_st = st;//在BB中对Container这个模板中的_st变量进行赋值
		cout << Container::_st << endl;//打印这个值
	}
};
int AA::_st = 1;// 静态成员变量的声明(静态成员变量的声明是只能在全局域)
void test4()
{
	BB<AA> b;//Container是AA
	b.fun(10);
}

解释在注释中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
将以下程序补充完整 #include <iostream> #include <fstream> #include <iomanip> using namespace std; template <typename ElemType> class myArrayList { private: int mSize; int mLen; ElemType *mpArr; public: myArrayList(int n); myArrayList(ElemType *a,int n); void show(); ElemType getMax(); //以下函数由同学完成 //void sort(); //myArrayList(myArrayList<ElemtType> &other);//拷贝构造函数 //mymyArrayList<ElemType> & operator =(mymyArrayList<ElemType> &other) }; template <typename ElemType> myArrayList<ElemType>::myArrayList(int n) { this->mSize=n; this->mLen=0; this->mpArr=new ElemType[mSize]; } template <typename ElemType> myArrayList<ElemType>::myArrayList(ElemType *a,int n) { this->mSize=n; this->mLen=n; this->mpArr=new ElemType[mSize]; for(int i=0;i<mLen;i++) mpArr[i]=a[i]; } template <typename ElemType> void myArrayList<ElemType>::show() { for(int i=0;i<mLen;i++) cout<<setw(4)<<mpArr[i]; cout<<endl; } template <typename ElemType> ElemType myArrayList<ElemType>::getMax() { ElemType max; max=mpArr[0]; for(int i=1;i<mLen;i++) if(max<mpArr[i]) max=mpArr[i]; return max; } //Student.h class Student { private: int mId; float height; int score; public: Student(int id=0,float h=0,int s=0):height(h),mId(id),score(s) { } friendbool operator <(Student &a1,Student &a2) { if(a1.height<a2.height) return true; else return false; } friend ostream &operator <<(ostream &out,Student &s) { out<<"ID:"<<s.mId<<" Height:"<<s.height<<" Socre:"<<s.score<<endl; return out; } }; //主程序 int main() { int a[]={1,2,3,5,7,9,12,8}; double b[]={1,2.5,3.6,5,7,9,12.8,8}; myArrayList <int> list1(a,8); list1.show(); cout<<"max="<<list1.getMax()<<endl; myArrayList <double> list2(b,8); list2.show(); cout<<"max="<<list2.getMax()<<endl; Student s[3]={Student(1,175,80),Student(2,178,90),Student(3,195,83)},s1; myArrayList <Student> list3(s,3); list3.show(); cout<<"max="<<list3.getMax()<<endl; } 说明: 将以下程序补充完整 //void sort(); //myArrayList(myArrayList<ElemtType> &other);//拷贝构造函数 //mymyArrayList<ElemType> operator =(mymyArrayList<ElemType> &other) //修改补充 Student类,要求按成绩排序(从高到低)
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

失去梦想的小草

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值