• class composition


    #include<iostream>
    using namespace std;
    
    class Composition{
        public:
            //同时有类内初始值和初始化列表时候,最终值为初始化列表中的
            Composition():im(this),m(20){cout<<"Composition ctor 
    ";}
        private:
            int m=9;//类内初始值
    
            //内部定义类
            //可访问私有成员
            class Inside{
                public:
                    Inside(Composition* cp){
                        cout<<"Inside ctor 
    ";
                        cp->m++;            //visit private member
                        cout<<cp->m<<endl;  //21
                    }
            };
            Inside im;
    };
    
    class Composition2{
        public:
            Composition2():im(this),m(20){cout<<"Composition2 ctor 
    ";}
        private:
            class Inside2{
                public:
                    Inside2(Composition2* cp){
                        cout<<"Inside2 ctor 
    ";
                        cp->m++;            //visit private member
                        cout<<cp->m<<endl;  //random number,成员初始化顺序导致
                    }
            };
            Inside2 im;
            int m=9;
    };
    int main(){
        Composition cp;
                        //Inside ctor
                        //21
                        //Composition ctor
    
        Composition2 cp2;
                        //Inside2 ctor
                        //1
                        //Composition2 ctor
        return 0;
    }
    
    
  • 相关阅读:
    Python基础07
    python基础06
    python基础05
    python基础04
    python基础03
    python基础02
    python组件之wtforms
    PythonWeb框架之Flask
    Linux下yum安装Redis
    在vuex的mutations中使用vue的小技巧
  • 原文地址:https://www.cnblogs.com/Searchor/p/13983458.html
Copyright © 2020-2023  润新知