• <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>

            linux&c++ R&D

            programing is a pleasure!

            Permission problem on a derived private method from public method

            problem:
            Is there a good explanation why compilation tags error for the call to
            AA.method(). It is public in the base class. If cast to baseclass,
            then compiler says it's ok.

            class A 


            public
                A() 
            {} 
                virtual 
            ~A() {} 
                virtual 
            void method(void{ std::cout << "A::method" <<std::endl; } 
            }
            ;// A 
            class AA : public A 
            public
                AA() 
            {} 
                virtual 
            ~AA() {} 
            private
                virtual 
            void method(void
                   A::method(); 
                   std::cout 
            << "AA::method" << std::endl; 
                }
             
            }
            ;// A 
            int main() 

                AA obj; 
                A
            * p = &obj; 
                p
            ->method(); 
                
            //((A&)obj).method(); 
                obj.method();                  // ERROR: 
             }


            gcc -o drvd drvd.cpp -lstdc++ -lm
            drvd.cpp: In function `int main()':
            drvd.cpp:26: error: `virtual void AA::method()' is private
            drvd.cpp:46: error: within this context
            http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/7ae23191ff4de6a9/4560e82948e9b91e#4560e82948e9b91e

            solution:
                 Above ,it seems as if it is a paradox !
                 why in devived class,function method() is declared as a private memeber,still in the way
                  A* p = &obj; 
                p
            ->method(); 
               it is right!
              The phenomena is explained in the Inside the C++ object model!
              In fact,p->method() is checked in two phases!
             At complie time,p->method()
             According to p type,check method()'s access level and method()'s validity,such as membership!
            so p->method() is right!
            At run time,
            p->method() is called in polymorphism way according to p's RTTI information!
            extending:
            Although it looks strange,maybe we can employ it!
            for example:
            class A{
            public:
            virtual void method()=0;
            }
            class AA:public A{
            private:
            virtual void method(){
            ...
            }
            };
            Obviously,
            A is interface and AA is a implemention!
            In the way,if you use AA directly,
            it will result in error,so prevent any subclass's use!
            A is the only entry!
            It may be helpful in some library's device!


            posted on 2007-04-21 12:44 丑石 閱讀(707) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++ problem and solution

            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊(cè)

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久综合日本熟妇| 无码国内精品久久综合88 | 久久伊人影视| 国产亚洲精久久久久久无码| 成人国内精品久久久久影院| 久久激情亚洲精品无码?V| 亚洲AV无码久久精品色欲| 久久91这里精品国产2020| 亚洲中文久久精品无码| 一本久久a久久精品vr综合| 人人狠狠综合久久亚洲高清| 麻豆精品久久精品色综合| 99久久国语露脸精品国产| 午夜精品久久久久9999高清| 2021精品国产综合久久| 亚洲国产精品一区二区三区久久| 日韩精品久久久肉伦网站| 日韩美女18网站久久精品| 久久精品中文字幕无码绿巨人| 草草久久久无码国产专区| 久久人人爽人人爽人人AV东京热| 久久99精品免费一区二区| 久久天堂AV综合合色蜜桃网| 久久免费大片| 2021国产成人精品久久| 国产精品美女久久久m| 国内高清久久久久久| 天天影视色香欲综合久久| 免费观看久久精彩视频| 精品久久久久香蕉网| 欧美噜噜久久久XXX| 久久经典免费视频| 久久夜色精品国产亚洲av| 办公室久久精品| 久久精品亚洲福利| 久久免费美女视频| 伊人久久综在合线亚洲2019| 久久99免费视频| 伊人久久无码中文字幕| 亚洲va中文字幕无码久久不卡| 亚洲国产精品嫩草影院久久|