• <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++ 技術(shù)中心

               :: 首頁 :: 聯(lián)系 ::  :: 管理
              160 Posts :: 0 Stories :: 87 Comments :: 0 Trackbacks

            公告

            鄭重聲明:本BLOG所發(fā)表的原創(chuàng)文章,作者保留一切權(quán)利。必須經(jīng)過作者本人同意后方可轉(zhuǎn)載,并注名作者(天空)和出處(CppBlog.com)。作者Email:coder@luckcoder.com

            留言簿(27)

            搜索

            •  

            最新隨筆

            最新評論

            評論排行榜

            // testss.cpp : 定義控制臺應(yīng)用程序的入口點。
            //

            #include "stdafx.h"
            #include <string>

            using namespace  std;
            char& testfun(char &m)
            {
                return m;
            }

            class CTextBlock
            {
            public:
                int length() const;

                const char &operator[](int pos) const
                {
                   //其他代碼
                    return pText[pos];
                }
                
                charoperator[](int pos)
                {
                    return const_cast<char&>( static_cast<const CTextBlock>(*this)[pos]  );
                }
            private:
                char *pText;
                mutable int textLength; //
                mutable bool lengthIsValid;
            };

            int CTextBlock::length() const
            {
                if(!lengthIsValid)
                {
                    textLength = strlen(pText);
                    lengthIsValid = true;
                }

                return textLength;
            }


            template<class T>
            class NameObject{
            public:
                NameObject(string &name,const T&value):nameValue(name),objectValue(value)
                {

                }

            private:
                std::string &nameValue;
                const T objectValue;
            };

            int _tmain(int argc, _TCHAR* argv[])
            {


                //1.const位置使用
                
            //以下a1,a2等同
                const int a1 = 10;
                int const a2=10;

                //以下b1,b2等同
                int const *b1 = NULL;
                const int *b2 = NULL;

                //指針c不允許改變
                int * const c = NULL;

                //2.返回引用問題
                
            //必需返回char &,否則返回char則不能被賦值
                char m = 'a';
                testfun(m) = 'p';

                //3.mutable(可變的)
                const CTextBlock textBlock;
                //在length中用mutable修飾textLength和lengthIsValid,
                
            //所以const函數(shù)length才可以訪問非const成員變量
                textBlock.length();
                

                //4.調(diào)用非const[]時,調(diào)用const類型的[]重載
                
            //詳細請參見[]重載
                const char& pChar=textBlock[0];
                CTextBlock textBlock2;
                char& tChar = textBlock2[0];

                //5.類的成員,盡可能的使用初始化列表一一初始化,
                
            //比賦值效率更高,也可以預(yù)防不明確行為的潘多拉盒子
                
            //父類比子類先初始化,類成員初始化順序是按變量聲明的順序,
                
            //即使初始化成員列表用其他順序。

                
            //6.為避免“跨編譯單元之初始化次序”問題,請以local static對象替換
                
            //non-local的static對象(local static即函數(shù)內(nèi)的,其他的稱為non-local static)
                
            //例如,單件返回static 對象引用的方法
                
                
            //7.編譯器自動生成構(gòu)造函數(shù),拷貝構(gòu)造,=運算符重載,非virtual的析構(gòu)函數(shù)。
                
            //如果有編寫構(gòu)造函數(shù)時,編譯器不會自動生成默認構(gòu)造和拷貝構(gòu)造函數(shù)。同樣定義
                
            //拷貝構(gòu)造也不會生成默認構(gòu)造函數(shù),但會生成=,析構(gòu)函數(shù)( 但在有被使用的時候才會生成)

                
            //8.編譯器自動生成“=”運算符重載時的特殊問題
                string newDog("persephone");
                string oldDog("stach");
                NameObject<int> p(newDog,2);
                NameObject<int> s(oldDog,36);
                
                //以下這種情況編譯器是不會自動生成賦值“=”構(gòu)造的,原因是
                
            //在一個內(nèi)含引用成員的或const成員時,編譯器不知道如何自動生成
                
            //賦值函數(shù)內(nèi)的實現(xiàn)。還有一種情況是,如果父類"="函數(shù)定義為private
                
            //編譯器同樣在子類中不會自動生成賦值函數(shù)。
                p = s;//該行語法錯誤

                return 0;
            }
            posted on 2014-11-14 13:29 C++技術(shù)中心 閱讀(2282) 評論(0)  編輯 收藏 引用 所屬分類: C++ 基礎(chǔ)
            久久久久久久综合狠狠综合| 国产精品免费久久久久电影网| 人妻无码久久精品| 欧美一区二区久久精品| 亚洲AV无码1区2区久久 | 人人狠狠综合久久亚洲| 久久精品国产2020| 久久久久国产一级毛片高清版| 久久人妻少妇嫩草AV无码蜜桃| 久久香蕉国产线看观看精品yw| 久久精品国产一区二区三区日韩| 中文字幕久久精品| 一级做a爰片久久毛片16| 精品国产乱码久久久久软件| 久久九九有精品国产23百花影院| 亚洲国产精品无码久久九九| 久久国产精品无码HDAV| 无码精品久久一区二区三区| 国产免费久久精品99久久| 色综合久久久久久久久五月| 久久天天躁狠狠躁夜夜不卡| 久久精品麻豆日日躁夜夜躁| 久久精品国产色蜜蜜麻豆| 激情综合色综合久久综合| 久久精品国产精品亚洲精品| 色欲综合久久中文字幕网| 99久久综合国产精品免费| 久久乐国产精品亚洲综合| 狠狠久久综合伊人不卡| 91精品国产色综久久 | 97久久精品人妻人人搡人人玩 | 少妇高潮惨叫久久久久久| 久久亚洲AV无码西西人体| 亚洲国产精品久久久久婷婷老年| 久久久久亚洲精品无码蜜桃| 奇米综合四色77777久久| 99精品国产免费久久久久久下载| 久久天天躁狠狠躁夜夜avapp | 久久精品国产一区| 亚洲午夜久久久精品影院| 99久久99久久精品国产片|