zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
  • 相关阅读:
    redis quick start
    Distributed processing
    DocFetcher 本机文件搜索工具
    ZeroTier One
    windows下搭建voip服务器
    在公司上wifi
    屏幕录制软件
    openresty vs golang vs nodejs
    DISC测试
    How to implement a windbg plugin
  • 原文地址:https://www.cnblogs.com/Searchor/p/13983458.html
Copyright © 2011-2022 走看看