• C++:初始化列表


    C++:初始化列表


    当我们的类中存在const成员我们该怎么对const成员进行初始化呢?

    ​ 我们可以通过初始化列表对const成员进行初始化

    ClassName :: ClassName () :m1(v1),m2(v1,v2),m3(v3){
    
    }
    

    1.初始化列表的使用

    #include <iostream>
    
    using namespace std;
    
    class Test{
    private:
        const int ci;
    public:
        Test():ci(10){
        }
        int GetCI(){
        return ci;
        }
    
    };
    int main(){
    
    Test t;
        cout << "CI = " << t.GetCI() << endl;
    
    return 0;
    }
    

    2.初始化列表的顺序

    ​ 初始化顺序与声明的顺序相同。

    ​ 成员的初始化顺序与初始化列表中的位置无关。

    ​ 初始化列表先于构造函数的函数体执行。

    #include <iostream>
    
    using namespace std;
    
    class Value{
    private:
        int m1;
    public:
        Value(int i){
            cout << "i=" << i << endl;
            m1 = 1;
        }
        int GetI(){
        return m1;
        }
    };
    
    class Test{
    private:
        Value m2;
        Value m1;
        Value m3;
    public:
        Test():m1(1),m2(2),m3(3){
    
        }
    };
    
    int main(){
    
    Test t;
    return 0;
    }
    
    

    3.类中的const成员

    ​ 类中的const成员会被分配空间。---当对象分配在哪,const成员就会分配在哪

    ​ 类中const成员的本质是只读变量。---因为我们可以通过指针去修改它

    ​ 类中const成员只能在初始化列表中指定初始化的值。---不信的话可以试试


    ​ 编译器无法直接得到const成员的初始值;

    ​ 因此无法进入符号表成为真正意义上的常量。

    #include <iostream>
    
    using namespace std;
    
    class Value{
    private:
        int m1;
    public:
        Value(int i){
            cout << "i=" << i << endl;
            m1 = 1;
        }
        int GetI(){
        return m1;
        }
    };
    
    class Test{
    private:
        const int ci;
        Value m2;
        Value m1;
        Value m3;
    public:
        Test():m1(1),m2(2),m3(3),ci(120){
        cout << "Test:test" << endl;
        }
        int GetCI(){
        return ci;
        }
    
        void SetCI(int v){
        int *p = const_cast<int *>(&ci);
        *p = v;
    
        }
    };
    
    int main(){
    
    Test t;
    
        cout << "GetCI = " << t.GetCI() << endl;
    
        t.SetCI(9);
    
        cout << "GetCI = " << t.GetCI() << endl;
    
    
    return 0;
    }
    
    

    4.初始化和赋值的区别

    int i = 0;  //初始化
    
    i = 0;		//赋值
    

    小结

    1.类中可以使用初始化列表对成员进行初始化。

    2.初始化列表先于构造函数体执行。

    3.类中可以定义const成员变量。

    4.const成员变量必须在初始化列表中指定初值。

    5.const成员变量为只读变量。


    本篇文章来自于个人学习记录,若有纰漏还请指正。

    编译环境:codeblock


    致我的家人和朋友.感谢他们陪我走过人生中最困惑的时光。

  • 相关阅读:
    Eigen SSE兼容,内存分配,和std容器的兼容理解
    Qt 进程守护程序(windows、linux)
    c++ vector 大数加法
    C#整合ActiveMQ与SpringBoot整合ActiveMQ数据间交互
    vue.js3:使用clipboard.js实现复制到剪贴板(vue@3.2.37 / clipboard@2.0.11)
    vue.js3: html的十六进制和rgb颜色互相转换(vue@3.2.37)
    spring boot: 用pinyin4j把中文转换为汉语拼音(spring boot v2.5.4)
    vue.js3: base64编码/解码(vue@3.2.37)
    vue.js3:封装一个选择国家或地区的component(vue@3.2.37)
    vue.js3: 使用elementplus的icon图标(vue@3.2.37 / @elementplus/iconsvue@2.0.6)
  • 原文地址:https://www.cnblogs.com/zhouhaocheng---yijianqinxin/p/13569147.html
Copyright © 2020-2023  润新知