const 修饰类对象的时候,表示常量对象,此时,不能调用类中的非const函数!非const函数指原型声明中结尾不加const的函数!
class A{
...
public:
int GetID() const
{...}
void SetID(int id)
{...}
};
...
const class A a;
a.GetID(); // 没问题
// a.SetID(100); // 不能调用非const函数
const 修饰iterator迭代器,表示迭代器是常量迭代器,不能对迭代器进行加减(++, --)等重指向操作(* const)
const vector<int>::iterator it = vecInt.begin();
...
// it++; // 非法操作
const_iterator 表示迭代器所指向的对象是常量(内置类型或class),此时可以对迭代器进行增减,但是不能修改所指向的值或对象的值(const *)
vector<int>::const_iterator cit = vecInt.begin();
for (; cit != vecInt.end(); cit++;)
{
// (*cit)++; // 非法操作
}
const 修饰容器本身时,表示容器本身不可更改,例如,不能进行push_back, pop_back等改变容器本身的操作;但可以改变容器中的内容
const vector<int> cvecInt = vecInt; //初始化
cvecInt[0] = 100; // 允许修改
// cvecInt.push_back(900); // 非法操作,不允许修改容器本身
完整例子
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student
{
private:
int id;
string name;
public:
Student()
{
}
int GetID() const
{
return id;
}
void SetID(const int o_id)
{
id = o_id;
}
string GetName() const
{
return name;
}
void SetName(const string o_name)
{
name = o_name;
}
friend ostream& operator<<(ostream &out, const Student& rhs)
{
out << "ID = " << rhs.id << " Name = " << rhs.name << endl;
return out;
}
};
int main()
{
vector<int> vecInt;
vecInt.push_back(2);
vecInt.push_back(5);
vecInt.push_back(3);
vector<int>::iterator it;
for (it = vecInt.begin(); it != vecInt.end(); ++it)
{
cout << *it << endl;
(*it)++;
cout << *it << endl;
}
cout << ">--------------------------------------<\n";
vector<int>::const_iterator cit;
for (cit = vecInt.begin(); cit != vecInt.end(); ++cit)
{
cout << *cit << endl;
//(*cit)++; const_iterator表示迭代器指向的对象是常量
}
cout << ">--------------------------------------<\n";
const vector<int>::iterator ccit = vecInt.begin();
(*ccit)++;
cout << *ccit << endl;
//for (; ccit != vecInt.end(); ++ccit) // const iterator表示迭代器的指向是常量
//{
// cout << *ccit << endl;
// (*ccit)++;
//}
cout << ">--------------------------------------<\n";
const vector<int> cvecInt(vecInt.begin(), vecInt.end());
// cvecInt.push_back(2); cvecInt本身不可更改,但内部的元素可以更改
vector<int>::const_iterator c_it;
for (c_it = cvecInt.begin(); c_it != cvecInt.end(); ++c_it)
{
cout << *c_it << endl;
}
cout << ">--------------------------------------<\n";
vector<Student> vecStu;
vecStu.push_back(Student());
vecStu.push_back(Student());
vecStu.push_back(Student());
vecStu[0].SetID(100);
vecStu[0].SetName("Student100");
vecStu[1].SetID(200);
vecStu[1].SetName("Student200");
vecStu[2].SetID(300);
vecStu[2].SetName("Student300");
vector<Student>::iterator sit;
for (sit = vecStu.begin(); sit != vecStu.end(); ++sit)
{
cout << "ID = " << (*sit).GetID() << " Name = " << (*sit).GetName() << endl;
}
cout << ">--------------------------------------<\n";
vecStu[0].SetID(1000);
vecStu[0].SetName("Student1000");
vecStu[1].SetID(2000);
vecStu[1].SetName("Student2000");
vecStu[2].SetID(3000);
vecStu[2].SetName("Student3000");
vector<Student>::const_iterator scit;
for (scit = vecStu.begin(); scit != vecStu.end(); ++scit)
{
cout << *scit;
//scit->SetID(1000);
cout << "After set ID:\n";
cout << "ID = " << (*scit).GetID() << " Name = " << (*scit).GetName() << endl;
}
cout << ">--------------------------------------<\n";
const Student stu;
stu.GetID();
//stu.SetID(1);
stu.GetName();
//stu.SetName("lalala");
const Student* pStu = new Student();
pStu->GetID();
//pStu->SetID(3);
pStu->GetName();
//pStu->SetName("bbabba");
system("pause");
return 0;
}