C++ 模板中的CONST

来源:百度知道 编辑:UC知道 时间:2024/09/22 12:30:41
template <typename Type> int SeqList<Type>::Find(Type x) const{
for(int i=0;i<m_ncurrentsize;i++)
if(m_elements[i]==x)
return i;
cout<<"can't find the element you want to find"<<endl;
return -1;

}

--------------------------------------------------------------
template <typename Type> int SeqList<Type>::Find(Type x) const{
其中的CONST是什么意思呢 》?

const 限定符限定
最常用的: const int max = 100;
定义一个常量。

如果一个函数这样定义:
void find(const int& x ) const {
那么,参数里的const表示传递给函数的值仅仅是x变量的一个复制,find函数里不会修改x的值。
参数列表后面的const是用来修饰函数的,表示函数内部不会修改其他变量的值,也就是只读变量,不写变量。你所问到的const就是这个意思。

常量函数,应该和模板没关系。 也就是说在这个函数中只能读,不能修改数据。