C++类中的const使用

简介: C++类中的const使用

一,const成员函数

1,const成员函数的语法

       将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数 隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。


定义方式和使用如下图:

ac935400183e46ed845aa97a32bc5a2e.png



       总的来说,当修饰this指针指向对象的成员时,只需在函数后面加上const即可。


2,相同限定符之间的调用

       这里要说明的是const与const修饰的对象和函数是可以相互调用的;非const与非const修饰的对象和函数也是可以相互调用的。


#include <iostream>
using namespace std;
class Date
{
public:
  Date(int year = 1, int month = 1, int day = 1)
  {
  _year = year;
  _month = month;
  _day = day;
  }
  void Print1() const {
  cout << _year << "年" << _month << "月" << _day << endl;
  this->Print2();//调用const成员函数Print2
  }
  void Print2() const{
  cout << _year << "年" << _month << "月" << _day << endl;
  }
  void Print3() {
  cout << _year << "年" << _month << "月" << _day << endl;
  this->Print4();//调用非const成员函数Print4
  }
  void Print4() {
  cout << _year << "年" << _month << "月" << _day << endl;
  }
private:
  int _year;
  int _month;
  int _day;
};
int main()
{
  Date d1(2023, 10, 16);
  d1.Print3();
  const Date d2(2022, 10, 16);
  d2.Print1();
  return 0;
}

       3,const成员函数内可以调用其它的非const成员函数吗?


       4,非const成员函数内可以调用其它的const成员函数吗?


3,不同限定符对象与函数的调用

       首先我们先观察const对象调用非const成员函数和非const对象调用const成员函数两个问题。

#include <iostream>
using namespace std;
class Date
{
public:
  Date(int year = 1, int month = 1, int day = 1)
  {
  _year = year;
  _month = month;
  _day = day;
  }
  void Print() const {
  cout << _year << "年" << _month << "月" << _day << endl;
  }
  void print() {
  cout << _year << "年" << _month << "月" << _day << endl;
  }
private:
  int _year;
  int _month;
  int _day;
};
int main()
{
  Date d1(2023, 10, 16);
  d1.Print();//正常运行
  const Date d2(2022, 10, 16);
  d2.print();//运行报错
  return 0;
}


分析:


       可发现,const对象不可以调用非const成员函数,但非const对象可以调用const成员函数。这里其实还是设计到权限放大与缩小的知识——权限可以缩小,但权限不能放大。


       当const对象调用非const成员函数时,相当于权限放大。对于const对象而言,其所有成员函数都不能修改对象成员的数值,但非const成员函数可以修改对象成员的数值,将原对象的权限放大。


       当非const对象调用const成员函数时,相当于权限缩小。对于非const对象而言,其所有成员的数值都可以被修改,const成员函数只是限定了此函数中对象成员的数值不可被修改,没有违反规定,只是将权限缩小而已。


4,不同限定符函数之间的调用

      有了以上的知识再来思考const成员函数调用其它的非const成员函数和非const成员函数调用其它的const成员函数,不难理解,非const成员函数是可以调用const成员函数,但const成员函数是不能调用非const成员函数,这跟对象调用函数时的情况一样。


class Date
{
public:
    Date(int year = 1, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    void Print1() const {
        this->Print4();//系统报错
    }
    void Print2() const{
    }
    void Print3() {
        this->Print2();//正常运行
    }
    void Print4() {
    }
private:
    int _year;
    int _month;
    int _day;
};


分析:


       当非const成员函数调用const成员函数时,非const成员函数对象成员的数值可以修改,当调用const成员函数时,在const成员函数内部中将权限缩小,内部对象成员的数值都不可修改,没有违反语法规定,因此可以被调用。


       当const成员函数调用非const成员函数时,主体const成员函数已经被限定权限了,当调用非const成员函数时,在const成员函数内部中将权限放大,内部对象成员的数值都可以修改,违背了const的限定,所以不能调用。


总结:const的调用满足权限可以缩小但不可以放大,也就是说非const修饰的对象或函数可以调用const修饰的对象或函数。但const修饰的对象或函数不可调用非const修饰的对象或函数。


相关文章
|
1天前
|
测试技术 C++
C++|运算符重载(3)|日期类的计算
C++|运算符重载(3)|日期类的计算
|
3天前
|
C语言 C++ 容器
C++ string类
C++ string类
9 0
|
3天前
|
C++ Linux
|
3天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
9 1
|
3天前
|
算法 安全 程序员
【C++】STL学习之旅——初识STL,认识string类
现在我正式开始学习STL,这让我期待好久了,一想到不用手撕链表,手搓堆栈,心里非常爽
16 0
|
3天前
|
存储 安全 测试技术
【C++】string学习 — 手搓string类项目
C++ 的 string 类是 C++ 标准库中提供的一个用于处理字符串的类。它在 C++ 的历史中扮演了重要的角色,为字符串处理提供了更加方便、高效的方法。
18 0
【C++】string学习 — 手搓string类项目
|
3天前
|
Java C++ Python
【C++从练气到飞升】06---重识类和对象(二)
【C++从练气到飞升】06---重识类和对象(二)
|
3天前
|
编译器 C++
【C++从练气到飞升】06---重识类和对象(一)
【C++从练气到飞升】06---重识类和对象(一)