zoukankan      html  css  js  c++  java
  • C++ 嵌套类使用(二)

    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指明作用域就可以,而且在嵌套类中,可以定义静态的成员。

  • 相关阅读:
    有继承的C++析构函数一定要用virtual
    CUDA vs2010配置
    lambda calculus(1)
    SICP练习1.6 1.16 解答
    用函数式来实现集合
    osx guile编译安装
    skiplist poj2892
    [转]理解 pkgconfig 工具
    专业术语解释
    【转】如何学习linux设备驱动
  • 原文地址:https://www.cnblogs.com/lidabo/p/3772478.html
Copyright © 2011-2022 走看看