C++从入门到精通——const与取地址重载

慈云数据 6个月前 (05-13) 技术支持 36 0

const与取地址重载

  • 前言
  • 一、const
    • 正常用法
    • const成员函数
    • 问题
      • `const`对象可以调用非`const`成员函数吗
      • 非`const`对象可以调用`const`成员函数吗
      • `const`成员函数内可以调用其它的非`const`成员函数吗
      • 非`const`成员函数内可以调用其它的`const`成员函数吗
      • 假如函数出现多个变量,const修饰的是谁
      • 总结
      • 二、取地址及const取地址操作符重载
        • 概念
        • 示例

          前言

          类的6个默认成员函数:如果一个类中什么成员都没有,简称为空类。

          空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。

          默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。

          class Date {};
          

          在这里插入图片描述


          一、const

          正常用法

          在C++中,可以使用const关键字来声明一个常量成员。常量成员是指在类中声明的成员变量被标记为只读,即不能在类的方法中进行修改。常量成员的值在对象创建时被初始化,并且在对象的整个生命周期中保持不变。

          常量成员的声明方式为在成员变量的类型前加上const关键字。例如:

          class MyClass {
          public:
              const int myConst = 10; // 常量成员的声明和初始化
          };
          

          在上述示例中,myConst被声明为一个常量成员,其初始值为10。

          const成员函数

          除了上面的这种用法外,C++在类里定义了新的const用法,将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。

          在这里插入图片描述

          我们来看看下面的代码

          class Date
          {
          public:
          	Date(int year, int month, int day)
          	{
          		_year = year;
          		_month = month;
          		_day = day;
          	}
          	void Print()
          	{
          		cout 
          		cout 
          	Date d1(2022, 1, 13);
          	d1.Print();
          	const Date d2(2022, 1, 13);
          	d2.Print();
          }
          
          public:
              void nonConstFunc() {
                  // 可以修改对象状态的非const成员函数
              }
              void constFunc() const {
                  // 不能修改对象状态的const成员函数
              }
          };
          int main() {
              const MyClass obj;
              obj.constFunc(); // 可以调用const成员函数
              // obj.nonConstFunc(); // 错误,不能调用非const成员函数
              return 0;
          }
          
          public:
              void nonConstFunc() {
                  // 可以修改对象状态的非const成员函数
              }
              void constFunc() const {
                  // 不能修改对象状态的const成员函数
              }
          };
          int main() {
              MyClass obj;
              obj.constFunc(); // 可以调用const成员函数
              obj.nonConstFunc(); // 也可以调用非const成员函数
              return 0;
          }
          
          public:
              void nonConstFunc() {
                  // 可以修改对象状态的非const成员函数
              }
              void constFunc() const {
                  // 不能修改对象状态的const成员函数
                  nonConstFunc(); // 错误! const成员函数不能调用非const成员函数
              }
          };
          int main() {
              MyClass obj;
              obj.constFunc();
              return 0;
          }
          
              const_cast
          public:
              void nonConstFunc() {
                  // 可以修改对象状态的非const成员函数
                  constFunc(); // 可以调用const成员函数
              }
              void constFunc() const {
                  // 不能修改对象状态的const成员函数
              }
          };
          int main() {
              MyClass obj;
              obj.nonConstFunc(); // 调用非const成员函数,它内部调用了const成员函数
              return 0;
          }
          
          public:
              void myFunction(int a, const int b) const;
          private:
              int m_variable;
          };
          
          public:
          	Date* operator&()
          	{
          		return this;
          	}
          	const Date* operator&()const
          	{
          		return this;
          	}
          private:
          	int _year; // 年
          	int _month; // 月
          	int _day; // 日
          };
          
          private:
              int value;
          public:
              MyClass(int value) : value(value) {}
              int* operator&() {
                  std::cout 
                  std::cout 
              MyClass obj(10);
              
              int* ptr1 = &obj;         // 调用非const版本的operator&
              const int* ptr2 = &obj;   // 调用const版本的operator&
              
              return 0;
          }
          
微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon