美文网首页
11.初始化列表

11.初始化列表

作者: lxr_ | 来源:发表于2021-02-02 12:47 被阅读0次
    #include<iostream>
    using namespace std;
    
    //初始化列表:用来初始化属性
    //语法:构造函数:属性1(值1),属性2(值2)....(){}
    class people
    {
    public:
        int m_A;
        int m_B;
        int m_C;
        //传统初始化操作
        /*people(int a, int b, int c)
        {
            m_A = a;
            m_B = b;
            m_C = c;
        }*/
        //初始化列表初始化属性
        people(int a,int b,int c) :m_A(a), m_B(b), m_C(c)
        {
    
        }
    };
    void test08()
    {
        //people p(10, 20, 30);
    
        people p(30,20,10);
    
        cout << "m_A:" << p.m_A << endl;
        cout << "m_B:" << p.m_B << endl;
        cout << "m_C:" << p.m_C << endl;
    }
    int main()
    {
        test08();
        
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:11.初始化列表

          本文链接:https://www.haomeiwen.com/subject/klwhtltx.html