const和迭代器

1、const在左边 - 指针指向的地址空间的内容不能改变,const在右边 - 指针本身的指向不能改变
2、迭代器

  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. int main() {
  5. //需要将vector开辟空间
  6. vector<int> vec(10,0);
  7. //T *const:指针指向不能变
  8. const vector<int>::iterator iter = vec.begin();
  9. *iter = 100;
  10. cout << *iter << endl;
  11. vector<int> vec2;
  12. for(int i = 0;i < 10;i++) {
  13. vec2.push_back(i);
  14. }
  15. //const T*:指针指向的内容不变
  16. vector<int>::const_iterator iter2 = vec2.begin();
  17. cout << *iter2 << endl;
  18. iter2++;
  19. cout << *iter2 << endl;
  20. return 0;
  21. }

const和函数返回值、函数参数

除非有需要修改的需求,否则将函数返回值和函数参数设置为const类型
例子:const修饰重载操作符函数的返回值和参数

  1. #include<iostream>
  2. using namespace std;
  3. class Rational {
  4. public:
  5. Rational() {}
  6. //或者
  7. //Rational() : m_num(0) {}
  8. Rational(int num) : m_num(num) {}
  9. int getNum() {
  10. return m_num;
  11. }
  12. //运算符重载作为类内函数的时候,二元运算符的参数只能是一个(因为隐含着一个参数)
  13. //如果二元运算符的参数一定为两个时,就必须是全局函数
  14. //因为使用了类内私有成员,必须用friend修饰符
  15. friend const Rational operator* (const Rational &lhs,const Rational &rhs);
  16. private:
  17. int m_num;
  18. };
  19. //const修饰函数返回值和函数参数
  20. const Rational operator* (const Rational &lhs,const Rational &rhs) {
  21. Rational ra;
  22. ra.m_num = lhs.m_num * rhs.m_num;
  23. return ra;
  24. }
  25. int main() {
  26. Rational a(3);
  27. Rational b(4);
  28. Rational c;
  29. c = a * b;
  30. cout << c.getNum() << endl;
  31. //a * b = 12,即又调用了一次operator=;是不被允许的,可能是想if(a * b == 12)但是写错了
  32. //用户自定义类型不能和内置类型不兼容,内置类型不允许发生a * b = xx的情况
  33. return 0;
  34. }

const成员函数

改善C++程序效率:函数传参 - 常对象,可以避免拷贝构造的发生,那么为了兼容const对象就需要const成员函数。const成员函数是为了兼容const对象而存在的,也就是说const成员函数不能修改const对象的成员变量。
两个成员函数如果只是常量性不同,是可以被重载的
1、const成员函数和const修饰函数返回值

  1. void func() const; //常成员函数:兼容常对象,不能修改常对象的成员变量
  2. const char &func(); //不能修改函数的返回值,也就是func() = func() + 1;是不被允许的,使得函数表达式不能为左值

为什么用size_t,而不是int,usigned int,unsigned long?
2、

  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4. class TextBook {
  5. public:
  6. //常对象可以调用构造函数
  7. TextBook(string m_text) : text(m_text) {}
  8. char &operator[](size_t position) {
  9. return text[position];
  10. }
  11. //返回值为const的原因:常对象时,string类型为const,string的每一位是不能改变的,即每一位为const char
  12. const char &operator[](size_t position) const {
  13. return text[position];
  14. }
  15. private:
  16. string text;
  17. };
  18. //const对象多用于传常引用,就需要可以对常对象进行操作的const成员函数
  19. void print(const TextBook &ctb) {
  20. cout << ctb[1] << endl;
  21. }
  22. int main() {
  23. TextBook tb("Hello");
  24. const TextBook ctb("World");
  25. cout << tb[0] << endl;
  26. cout << ctb[0] << endl;
  27. //const成员函数不能修改常对象的成员变量
  28. tb[0] = 'A';
  29. cout << tb[0] << endl;
  30. //ctb[0] = 'B'; //报错
  31. print(ctb);
  32. return 0;
  33. }

注:1)调用[]是没有问题的,但是对其const重载函数进行赋值是错误的
2)如果重载函数返回值是char,而不是char&,那么tb[0] = ‘A’的修改操作是错误的,修改的是拷贝的副本而不是tb[0]本身
3)为什么不能是char *text呢?

bitwise constness

bitwise constness:常成员函数中,不能更改对象内的任何一位
但是如果只有指针隶属于对象,那么更改了指针所指物也能通过编译器检验

  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4. class TextBook {
  5. public:
  6. TextBook(string m_text) {
  7. pText = (char *)m_text.c_str();
  8. }
  9. char& operator[](size_t position) const {
  10. return pText[position];
  11. }
  12. private:
  13. //只有常对象中的指针是const,但是指针所指物不是const
  14. char *pText;
  15. };
  16. int main() {
  17. const TextBook ctb("Hello");
  18. cout << ctb[0] << endl;
  19. //operator[]返回值是char *,所以用char *p来承接
  20. char *p = &ctb[0];
  21. *p = 'A';
  22. //更改了指针所指物
  23. cout << ctb[0] << endl;
  24. return 0;
  25. }

logical constness

当客户端侦测不出来时,常成员函数可以修改对象内部分成员变量,但是为了编译器不报错 - mutable

  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. using namespace std;
  5. class TextBook {
  6. public:
  7. TextBook(string m_text);
  8. size_t length() const;
  9. private:
  10. char* pText;
  11. mutable size_t textLength;
  12. mutable bool lengthIsValid;
  13. };
  14. TextBook::TextBook(string m_text) {
  15. textLength = 0;
  16. lengthIsValid = false;
  17. pText = (char *)m_text.c_str();
  18. }
  19. size_t TextBook::length() const {
  20. if(!lengthIsValid) {
  21. textLength = strlen(pText);
  22. lengthIsValid = true;
  23. }
  24. return textLength;
  25. }
  26. int main() {
  27. const TextBook ctb("Hello World!");
  28. cout << ctb.length() << endl;
  29. return 0;
  30. }

在const和non-const避免重复

1、在non-const里调用const是允许的
non-const转化为const对象调用const重载函数,再将其转为非const引用返回

  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include<string>
  4. #include<string.h>
  5. using namespace std;
  6. class TextBook {
  7. public:
  8. TextBook(string m_str) : str(m_str) {}
  9. char &operator[](size_t position) {
  10. //为了避免重复
  11. return const_cast<char &>(static_cast<const TextBook>(*this)[position]);
  12. }
  13. const char &operator[](size_t position) const {
  14. for(int i = 1;i <= str.size();i++) {
  15. cout << i << endl;
  16. }
  17. return str[position];
  18. }
  19. private:
  20. string str;
  21. int n;
  22. };
  23. int main() {
  24. const TextBook ctb("Hello world!");
  25. cout << ctb[1] << endl;
  26. TextBook tb("No Bug!");
  27. cout << tb[1] << endl;
  28. return 0;
  29. }

注:1)static_cast(this) - 将自身对象转换为常对象
static_cast(
this)[position] - 调用const重载函数
const_cast(static_cast(this)[position]) - 将结果去常
2)[
this和this](https://blog.csdn.net/stpeace/article/details/22220777#)
return *this:返回的是当前对象的本身(返回值类型为A&)或副本,对副本(临时值)取地址会报错
return this:返回的是当前对象的指针
2、const调用non-const是不允许的