• <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>
            隨筆 - 30, 文章 - 0, 評(píng)論 - 64, 引用 - 0
            數(shù)據(jù)加載中……

            VC6.0中重載操作符函數(shù)無(wú)法訪(fǎng)問(wèn)類(lèi)的私有成員

            在 C++ 中,操作符(運(yùn)算符)可以被重載以改寫(xiě)其實(shí)際操作。
            同時(shí)我們可以定義一個(gè)函數(shù)為類(lèi)的朋友函數(shù)(friend function)以便使得這個(gè)函數(shù)能夠訪(fǎng)問(wèn)類(lèi)的私有成員,
            這個(gè)定義通常在頭文件中完成。

            在Visual C++中定義一般的函數(shù)為朋友函數(shù)通常是沒(méi)有問(wèn)題的。
            然而對(duì)某些重載操作符的函數(shù),
            即使我們將它們定義為類(lèi)的朋友函數(shù),VC的編譯器仍然會(huì)顯示出錯(cuò)信息,
            認(rèn)為這些朋友函數(shù)無(wú)權(quán)訪(fǎng)問(wèn)類(lèi)的私有成員。
            我認(rèn)為這應(yīng)該是VC6.0的bug。

            以下代碼就是個(gè)例子:

            // 頭文件 “Sample.h”
                        #include<iostream>
                        using namespace std;
                        class Sample {
                        public:
                        Sample();
                        friend ostream &operator<<(ostream &out, const Sample s);
                        friend istream &operator>>(istream &in, Sample & s);
                        private:
                        int x;
                        };
            
            
            // 實(shí)現(xiàn)文件 “Sample.cpp”
                        #include “Sample.h”
                        Sample::Sample() {
                        x=0;
                        }
                        istream &operator>>(istream &in, Sample & s) {
                        cout<<”Please enter a value”<<endl;
                        in >> s.x ;
                        return in;
                        }
                        ostream &operator<<(ostream &out, const Sample s) {
                        cout << s.x << endl;
                        return out;
                        }

            以上代碼在gnuc++中編譯運(yùn)行毫無(wú)問(wèn)題。但是在VC++6.0中編譯的時(shí)候就會(huì)出現(xiàn)以下的編譯錯(cuò)誤:

            Compiling…
            Sample.cpp
            c:\temp\sample.cpp(8) : error C2248: ‘x’ : cannot access private member declared in class ‘Sample’
            c:\temp\sample.h(19) : see declaration of ‘x’
            c:\temp\sample.cpp(13) : error C2248: ‘x’ : cannot access private member declared in class ‘Sample’
            c:\temp\sample.h(19) : see declaration of ‘x’
            Error executing cl.exe.Sample.obj - 2 error(s), 0 warning(s)

            在VC++ 6.0中解決這個(gè)問(wèn)題有以下幾種方法:

            • 在頭文件中實(shí)現(xiàn)作為朋友函數(shù)的操作符函數(shù)的重載,也就是說(shuō)在實(shí)現(xiàn)文件”Sample.cpp”中將函數(shù)重載的實(shí)現(xiàn)去掉,而將頭文件修改如下:
              // 修改后的頭文件 1 “Sample.h”
                              #include<iostream>
                              using namespace std;
                              class Sample {
                              public:
                              Sample();
                              friend ostream &operator<<(ostream &out, const Sample s);
                              friend ostream &operator<<(ostream &out, const Sample s) {
                              cout << s.x << endl;
                              return out;
                              }
                              friend istream &operator>>(istream &in, Sample & s);
                              friend istream &operator>>(istream &in, Sample & s) {
                              cout<<”Please enter a value”<<endl;
                              in >> s.x ;
                              return in;
                              }
                              private:
                              int x;
                              };
              
                  
            • 在頭文件中類(lèi)定義之前將類(lèi)和朋友操作符函數(shù)的原型特別聲明一下,也就是將頭文件修改如下(實(shí)現(xiàn)文件”Sample.cpp”不用作任何修改):
              // 修改后的頭文件 2 “Sample.h”
                              #include<iostream>
                              using namespace std;
                              // 以下3行代碼為新加入
                              class Sample;
                              ostream &operator<<(ostream &out, const Sample s);
                              istream &operator>>(istream &in, Sample & s);
                              class Sample {
                              public:
                              Sample();
                              friend ostream &operator<<(ostream &out, const Sample s);
                              friend istream &operator>>(istream &in, Sample & s);
                              private:
                              int x;
                              };
              
                  
            • 第三種方法是對(duì)I/O名空間的使用實(shí)行明確聲明,也就是說(shuō)在頭文件”Sample.h”中直接寫(xiě):
              #include<iostream>
              using std::ostream;
              using std::istream
              ….
              取代 “using namespace std;”
              注意:在這個(gè)例子里我們?cè)趯?shí)現(xiàn)文件 “Sample.cpp”中包含 “using namespace std;”這句話(huà),否則在實(shí)現(xiàn)中就不能使用 “cout” , “cin”, “<< “, “>>” 和 endl 這些關(guān)鍵字和符號(hào)。修改后的完整代碼如下:

               

              // Sample.h
                              #include<iostream>
                              using std::istream;
                              using std::ostream;
                              class Sample {
                              public:
                              Sample();
                              friend ostream &operator<<(ostream &out, const Sample s);
                              /*friend ostream &operator<<(ostream &out, const Sample s) {
                              cout << s.x << endl;
                              return out;
                              }*/
                              friend istream &operator>>(istream &in, Sample & s);
                              /*friend istream &operator>>(istream &in, Sample & s) {
                              cout<<”Please enter a value”<<endl;
                              in >> s.x ;
                              return in;
                              }*/
                              private:
                              int x;
                              };
              // “Sample.cpp”
                              #include “Sample.h”
                              using namespace std;
                              Sample::Sample() {
                              x=5;
                              }
                              istream &operator>>(istream &in, Sample & s) {
                              cout<<”Please enter a value”<<endl;
                              in >> s.x ;
                              return in;
                              }
                              ostream &operator<<(ostream &out, const Sample s) {
                              cout << s.x << endl;
                              return out;
                              }
              
                  

            posted on 2008-12-08 23:50 henry08 閱讀(2374) 評(píng)論(5)  編輯 收藏 引用 所屬分類(lèi): C++

            評(píng)論

            # re: VC6.0中重載操作符函數(shù)無(wú)法訪(fǎng)問(wèn)類(lèi)的私有成員  回復(fù)  更多評(píng)論   

            能講解一下其內(nèi)在原因是什么嗎?
            2008-12-09 09:45 | abettor

            # re: VC6.0中重載操作符函數(shù)無(wú)法訪(fǎng)問(wèn)類(lèi)的私有成員  回復(fù)  更多評(píng)論   

            猜測(cè)的成分太多了,friend function好像不是翻成朋友函數(shù),叫友元吧
            2008-12-09 09:51 | zuhd

            # re: VC6.0中重載操作符函數(shù)無(wú)法訪(fǎng)問(wèn)類(lèi)的私有成員  回復(fù)  更多評(píng)論   

            visual c++ 2005 就支持了
            2008-12-10 21:45 | 漂漂

            # re: VC6.0中重載操作符函數(shù)無(wú)法訪(fǎng)問(wèn)類(lèi)的私有成員  回復(fù)  更多評(píng)論   

            hanxiaodkf
            2009-07-14 16:50 | han

            # re: VC6.0中重載操作符函數(shù)無(wú)法訪(fǎng)問(wèn)類(lèi)的私有成員[未登錄](méi)  回復(fù)  更多評(píng)論   

            Thanks
            2012-06-25 09:43 | yong
            粉嫩小泬无遮挡久久久久久| 久久人妻少妇嫩草AV蜜桃| 久久99精品久久久大学生| 国产免费久久精品99re丫y| 色99久久久久高潮综合影院| 久久这里只有精品首页| 久久久久人妻精品一区二区三区 | 四虎久久影院| 色综合久久无码五十路人妻| 91久久成人免费| 久久久久久精品久久久久| 精品久久久久久成人AV| 国产午夜精品久久久久九九电影| 亚洲精品无码久久久久AV麻豆| 久久天天躁狠狠躁夜夜avapp| 久久久久国色AV免费观看| 日韩乱码人妻无码中文字幕久久| 久久被窝电影亚洲爽爽爽| 国产A三级久久精品| 久久99精品免费一区二区| 亚洲AV无码久久精品蜜桃| 久久久99精品成人片中文字幕| 久久综合九色综合网站| 精品伊人久久久| 久久五月精品中文字幕| 久久99国产精品二区不卡| 亚洲精品蜜桃久久久久久| 久久91精品国产91| 久久久久亚洲AV综合波多野结衣| 国产欧美一区二区久久| AAA级久久久精品无码片| 久久亚洲精品无码AV红樱桃| 2021国产精品久久精品| 久久综合久久综合亚洲| 久久www免费人成精品香蕉| 91性高湖久久久久| 国产 亚洲 欧美 另类 久久| 99久久精品无码一区二区毛片 | 亚洲欧美一区二区三区久久| 亚洲国产成人精品91久久久 | 午夜福利91久久福利|