• <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) 評論(0)  編輯 收藏 引用 所屬分類: C++ problem and solution

            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲国产成人精品女人久久久 | 无码任你躁久久久久久老妇App| 久久99精品国产麻豆宅宅| 2021国产精品久久精品| 久久人人添人人爽添人人片牛牛 | 久久99精品久久久久久9蜜桃| 99久久久精品| 国产91色综合久久免费分享| 人妻无码久久一区二区三区免费| 色青青草原桃花久久综合| 一本久久免费视频| 久久精品亚洲AV久久久无码| 国色天香久久久久久久小说| 伊人久久大香线蕉av不变影院| 久久久精品国产sm调教网站| 久久国产乱子伦免费精品| 国产精品久久久久久久久免费| 热久久国产精品| 久久精品国产99久久香蕉| 亚洲国产精品嫩草影院久久| 伊人久久综合无码成人网| 久久久久无码精品国产不卡| 久久99热狠狠色精品一区| 久久精品无码一区二区三区| 久久精品国产秦先生| 国内精品久久久久久麻豆| 中文字幕精品久久| 国产精品一久久香蕉国产线看| 国产亚州精品女人久久久久久 | 久久久精品国产sm调教网站 | 日韩精品国产自在久久现线拍 | 精品久久久无码21p发布 | 日本道色综合久久影院| 亚洲国产精品综合久久一线 | 久久免费视频网站| 蜜臀久久99精品久久久久久| 久久综合国产乱子伦精品免费| 久久精品99无色码中文字幕| 亚洲第一极品精品无码久久| 久久久99精品一区二区| 成人久久精品一区二区三区|