<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    C++ 嵌套類使用(二)

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    C++嵌套類

    1   嵌套類的名字只在外圍類可見。

    2   類的私有成員只有類的成員和友元可以訪問,因此外圍類不可以訪問嵌套類的私有成員。嵌套類可以訪問外圍類的成員(通過對象、指針或者引用)。

    3   一個好的嵌套類設計:嵌套類應該設成私有。嵌套類的成員和方法可以設為 public 

    4   嵌套類可以直接訪問外圍類的靜態成員、類型名( typedef )、枚舉值。

    // qiantaolei.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include
    using namespace std;
    class MotherClass
    {
    public:
        MotherClass(int b)
        {
            a=b;
            cout<<"MotherClass constructed "<<endl;
        }
       int  fun();
      
       int getA();
    public:
        
        class mothersClass
        {
        public:
            mothersClass(int b)
            {
                mothersT =b;
                cout<<"MotherClass::mothersClass constructed "<<endl;
            }
            int  funT();
            int getmothersT();
            //int getMotherClassAA()
            //{
            //    return MotherClass::aa;
            //}
            //int getMotherClassBB()
            //{
            //    MotherClass::bb=1234;
            //    return MotherClass::bb;

            //}
        protected:
        private:
            int mothersT;
        };
    protected:

    public:

       
      

     


    private:
        int a;
    };//

    //static int MotherClass::aa =100;

    int MotherClass::fun()
    {
       mothersClass mothersClassT(1);
       return mothersClassT.getmothersT();

    }
    int MotherClass::getA()
    {
        //a= mothersClass::getmothersT();//error
      return a;
    }
    int MotherClass::mothersClass::getmothersT()
    {
        return mothersT;

    }
    int MotherClass::mothersClass::funT()
    {
        MotherClass MotherClassT(2);

        return MotherClassT.getA();
    }

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

        MotherClass myClass(3);
        MotherClass::mothersClass myClassT(4);
        MotherClass::mothersClass myClassTT(5);
        int a= myClass.getA();
        cout<<"MotherClass::getA()="<<a<<endl;
        cout<<"MotherClass::fun()="<<myClass.fun()<<endl;
        a= myClassT.getmothersT();
        cout<<"MotherClass::mothersClass::getmothersT()="<<a<<endl;
        a=myClassT.funT();
        cout<<"MotherClass::mothersClass::funT()="<<a<<endl;
       
        //cout<<"MotherClass::mothersClass.getMotherClassAA()="<<MotherClass::mothersClass.getMotherClassAA()<<endl;
        cin.get();

     return 0;
    }

     

    下面內容是從網上貼來的。

    下面內容是從網上貼來的。

     

     

     

    C++嵌套類

     嵌套類的訪問問題:

    記得白鳳煮的C++中有一句這樣的話:C++嵌套類只是語法上的嵌套。然而在實踐過程中,卻并非如此。

    Ex:

     class A

      {

     public: 

         static int a;

         class A1

          {

             void output()

              {

               cout<<a<<endl; //instead of A::a;

             }

         };

      

     };

     int A::a;

    可見,類 A1 嵌入A后訪問A的靜態變量不寫外圍域沒有任何問題,從編譯的角度看,此時位于A::的作用域內,在符號表中是可以找到a(注意,a必須為static)。這一點,與分開寫的兩個類明顯不同

     

    還有一個特殊之處,見如下代碼:

     

     Ex:

     

     class A

      {

     private:

         int a;

         class A1

          {

             void output(A aObject)

              {

               cout<<aObject.a<<endl; //instead of A::a;

             }

         };

      

     };

    這段代碼在VC中不能編譯通過,但在DEVC++是可以的,也就是不同的編譯對于嵌套類是否能訪問外圍類的私有成員的定義是不一致的。

    嵌套類的不同作用域同名問題:

     class A

      {

     public: 

         static int a;

         class A1

          {

             static int a;

             int    void output()

              {

               cout<<a<<endl; //instead of A::a;

             }

         };

      

     };

     int A::a=3;

     int A::A1::a=4;

    輸出內部的a沒有問題,如果要訪問外部的,使用A::a指明作用域就可以,而且在嵌套類中,可以定義靜態的成員。

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全