• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            c++實例研究

            從0開始

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              104 隨筆 :: 0 文章 :: 20 評論 :: 0 Trackbacks
            #include <iostream>
            #include 
            <cstdlib>
            using namespace std;

            class Foo
            {
            public:
                Foo(
            int n):val(n),changed(false){}
                
            int getVal()const
                
            {
                    Foo 
            *fp = const_cast<Foo*>(this);
                    fp
            ->changed=true;
                    
            //changed=true; //error: in read-only structure
                    return val;
                }
            ;
                
            bool isVisit()const{return changed;}
            private:
                
            int val;
                
            bool changed;
            }
            ;

            int main()
            {
                Foo f(
            10);
                cout
            <<f.isVisit()<<endl;
                cout
            <<f.getVal()<<endl;
                cout
            <<f.isVisit()<<endl;
                system(
            "PAUSE");
                
            return 0;
            }


            一種更好的方法是使用mutable關鍵字,表示即使在const情況下,仍然能被修改
            #include <iostream>
            #include 
            <cstdlib>
            using namespace std;

            class Foo
            {
            public:
                Foo(
            int n):val(n),changed(false){}
                
            int getVal()const
                
            {
                    changed
            =true//OK
                    return val;
                }
            ;
                
            bool isVisit()const{return changed;}
            private:
                
            int val;
                mutable 
            bool changed;
            }
            ;

            int main()
            {
                Foo f(
            10);
                cout
            <<f.isVisit()<<endl;
                cout
            <<f.getVal()<<endl;
                cout
            <<f.isVisit()<<endl;
                system(
            "PAUSE");
                
            return 0;
            }

            如果一個類中有很多需要被修改,可以單獨作為一個成員類
            #include <iostream>
            #include 
            <cstdlib>
            using namespace std;

            class Bar
            {
            public:
                Bar():changed(
            false),val_count(0){}
                
            bool changed;
                
            int val_count;
            }
            ;

            class Foo
            {
            public:
                Foo(
            int n):val(n),b(new Bar()){}
                
            int getVal()const
                
            {
                    b
            ->changed=true//ok
                    b->val_count++;
                    
            return val;
                }
            ;
                
            bool isVisit()const{return b->changed;}
                
            int getValCount()const{return b->val_count;}
            private:
                
            int val;
                Bar
            * b;
            }
            ;

            int main()
            {
                Foo f(
            10);
                cout
            <<f.isVisit()<<endl;
                cout
            <<f.getValCount()<<endl;
                
                cout
            <<f.getVal()<<endl;
                cout
            <<f.isVisit()<<endl;
                cout
            <<f.getValCount()<<endl;

                cout
            <<f.getVal()<<endl;
                cout
            <<f.isVisit()<<endl;
                cout
            <<f.getValCount()<<endl;

                system(
            "PAUSE");
                
            return 0;
            }


            特別注意上例中,指針b的初始化的寫法,其實,在構造函數冒號后的member(val)相當于member=val
            posted on 2010-05-01 11:10 elprup 閱讀(392) 評論(0)  編輯 收藏 引用 所屬分類: c++實例
            久久精品国产亚洲一区二区| 久久国产精品成人免费| 国产美女亚洲精品久久久综合| 国产偷久久久精品专区| 粉嫩小泬无遮挡久久久久久| 久久精品国产精品亜洲毛片| 久久久这里有精品| 国产毛片久久久久久国产毛片| 久久亚洲中文字幕精品一区四| 漂亮人妻被中出中文字幕久久| AV无码久久久久不卡网站下载| 久久影院亚洲一区| 久久婷婷国产综合精品 | 久久亚洲AV无码精品色午夜| 久久人人爽人人爽人人片AV不| 久久伊人影视| 久久最近最新中文字幕大全| 久久亚洲AV无码精品色午夜| 久久久久国产一级毛片高清板| 97久久国产亚洲精品超碰热| 99久久精品免费看国产一区二区三区| 韩国三级中文字幕hd久久精品| 久久香蕉超碰97国产精品| 久久笫一福利免费导航 | 国产精品久久久久影院嫩草| 久久久久亚洲av成人无码电影| 久久精品9988| 大香网伊人久久综合网2020| a级成人毛片久久| 久久久精品国产sm调教网站| 色欲久久久天天天综合网精品| 国产精品成人久久久| 久久影院亚洲一区| 色妞色综合久久夜夜| 精产国品久久一二三产区区别| 久久嫩草影院免费看夜色| 精品无码久久久久久久久久 | 尹人香蕉久久99天天拍| 人妻系列无码专区久久五月天| 日本亚洲色大成网站WWW久久| 伊人久久成人成综合网222|