Chinaunix首页 | 论坛 | 博客
  • 博客访问: 249234
  • 博文数量: 55
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 419
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-06 20:22
文章分类

全部博文(55)

文章存档

2014年(55)

我的朋友

分类: C/C++

2014-04-12 14:54:36

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 "<     }
   int  fun();
  
   int getA();
public:
    
    class mothersClass
    {
    public:
        mothersClass(int b)
        {
            mothersT =b;
            cout<<"MotherClass::mothersClass constructed "<         }
        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()="<     cout<<"MotherClass::fun()="<     a= myClassT.getmothersT();
    cout<<"MotherClass::mothersClass::getmothersT()="<     a=myClassT.funT();
    cout<<"MotherClass::mothersClass::funT()="<    
    //cout<<"MotherClass::mothersClass.getMotherClassAA()="<     cin.get();

 return 0;
}

 

下面内容是从网上贴来的。

下面内容是从网上贴来的。

 

 

 

C++嵌套类

 嵌套类的访问问题:

记得白凤煮的C++中有一句这样的话:C++嵌套类只是语法上的嵌套。然而在实践过程中,却并非如此。

Ex:

 class A

  {

 public: 

     static int a;

     class A1

      {

         void output()

          {

           cout<

         }

     };

  

 };

 int A::a;

可见,类 A1 嵌入A后访问A的静态变量不写外围域没有任何问题,从编译的角度看,此时位于A::的作用域内,在符号表中是可以找到a(注意,a必须为static)。这一点,与分开写的两个类明显不同

 

还有一个特殊之处,见如下代码:

 

 Ex:

 

 class A

  {

 private:

     int a;

     class A1

      {

         void output(A aObject)

          {

           cout<

         }

     };

  

 };

这段代码在VC中不能编译通过,但在DEVC++是可以的,也就是不同的编译对于嵌套类是否能访问外围类的私有成员的定义是不一致的。

嵌套类的不同作用域同名问题:

 class A

  {

 public: 

     static int a;

     class A1

      {

         static int a;

         int    void output()

          {

           cout<

         }

     };

  

 };

 int A::a=3;

 int A::A1::a=4;

输出内部的a没有问题,如果要访问外部的,使用A::a指明作用域就可以,而且在嵌套类中,可以定义静态的成员。


原文:http://blog.csdn.net/firstit/article/details/6154320

阅读(1465) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~