小工具知识网

当前位置: 首页 > 行业知识 > 【C++干货基地】面向对象核心概念 const成员函数 | 初始化列表 | explicit关键字 | 取地址重载

行业知识

【C++干货基地】面向对象核心概念 const成员函数 | 初始化列表 | explicit关键字 | 取地址重载

2026-11-05 22:19:09 来源:互联网转载
鸽芷咕:个人主页

  个人专栏: 《C++干货基地》《粉丝福利》 ⛺️生活的理想,就是为了理想的生活!


引入

  哈喽各位铁汁们好啊,我是博主鸽芷咕《C++干货基地》是由我的襄阳家乡零食基地有感而发,不知道各位的城市有没有这种实惠又全面的零食基地呢?C++ 本身作为一门篇底层的一种语言,世面的免费课程大多都没有教明白。所以本篇专栏的内容全是干货让大家从底层了解C++,把更多的知识由抽象到简单通俗易懂。

⛳️ 推荐

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。

文章目录

  • 引入
  • ⛳️ 推荐
  • 一、const 成员函数
    • 1.1 什么是const 成员函数
    • 1.2 const成员函数的注意事项
    • 总结
    • 二、取地址及const取地址操作符重载
      • 2.1 取地址操作的意义
      • 三、重新认识构造函数
        • 3.1构造函数体赋值
        • 3.2 初始化列表
          • 规则一
          • 规则二
          • 四、explicit关键字
            • 4.1 构造函数的隐式类型转换
            • 4.2 隐式转换的作用
            • 4.2 explicit关键字的使用

              一、const 成员函数

              1.1 什么是const 成员函数

              cosnt 的成员函数其实就是在我们 函数的括号外 多加一个 const void Dlsplay() const

              • 其他的作用是修饰 隐含的 this 指针,使其不能修改。

                1.2 const成员函数的注意事项

                const 成员可以直接修饰this指针那么使用起来有什么要注意的嘛?

                • 下面我们看一下这些代码来思考一下
                  #includeusing namespace std;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();}int main(){Test();return 0;}public:Date* operator&(){return this;}const Date* operator&()const{return this;}private:int _year; // 年int _month; // 月int _day; // 日};public:Date(int year, int month, int day) {     _year = year;     _month = month;     _day = day; }private:int _year = 2022;int _month = 06;int _day = 25;};public:Date(int year, int month, int day): _year(year), _month(month), _day(day){}private:int _year;int _month;int _day;};public:    A(int a)       :_a1(a)       ,_a2(_a1)   {}        void Print() {        cout    A aa(1);    aa.Print();}public:A(int x){this-_x = x;cout cout A a1(1);A a2 = 3;return 0;}public:Data(int year, int month = 10, int day = 1){cout cout Data a1(2021);Data a2 = 2022;return 0;}public://explicit C(int x = 0)C(int x = 0):_x(x){}C(const C& cc){cout private:// 缺省值int a = 1;int* p1 = nullptr;int* p2 = (int*)malloc(4);C cc1 = xx;  // 虽然可以,但是很费劲C cc2 = 2;};public://explicit C(int x = 0)C(int x = 0):_x(x){}C(const C& cc){cout public:void Push(const C& c){//}};int main(){Stack st;C cc3(3);st.Push(cc3);st.Push(4);return 0;}public:explicit A(int x){cout cout public:explicit Data(int year, int month = 10, int day = 1){cout cout A a1 = 2;Data a2 = 2020;return 0;}

上一篇:DNF吸怪基址编写技巧揭秘,核心要点轻松掌握!

下一篇:使用npm时一直idealTree:npm: sill idealTree buildDeps