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中不能编译通过,但在DEV-C++是可以的,也就是不同的编译对于嵌套类是否能访问外围类的私有成员的定义是不一致的。

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

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++ 嵌套类使用(二)的更多相关文章

  1. mfc 嵌套类

    嵌套类 一. 嵌套类 嵌套类的定义 将某个类的定义放在另一个类的内部,这样的类定义,叫嵌套类. class AAA { int aaa; class BBB { int bbb; //其它成员或者函数 ...

  2. C++学习笔记(十二):类继承、虚函数、纯虚函数、抽象类和嵌套类

    类继承 在C++类继承中,一个派生类可以从一个基类派生,也可以从多个基类派生. 从一个基类派生的继承称为单继承:从多个基类派生的继承称为多继承. //单继承的定义 class B:public A { ...

  3. (十二)java嵌套类和内部类

    嵌套类和内部类:在一个类里边定义的类叫做嵌套类,其中没有static修饰的嵌套类是我们通常说的内部类,而被static修饰的嵌套类不常用.有的地方没有嵌套类和内部类的区分,直接是嵌套类就称作内部类,没 ...

  4. Java 嵌套类和内部类演示样例&lt;二&gt;

    嵌套类(nested class)是一个在还有一个类或接口内部声明的类. 嵌套类分为两种:静态内部类(static inner class)和非静态嵌套类(non-static nested clas ...

  5. Scala 面向对象(十二):嵌套类

    在Scala中,你几乎可以在任何语法结构中内嵌任何语法结构.如在类中可以再定义一个类,这样的类是嵌套类,其他语法结构也是一样. 嵌套类类似于Java中的内部类. Scala嵌套类的使用1 请编写程序, ...

  6. JAVA 嵌套类和内部类

    一.什么是嵌套类及内部类?  可以在一个类的内部定义另一个类,这种类称为嵌套类(nested classes),它有两种类型:  静态嵌套类和非静态嵌套类.静态嵌套类使用很少,最重要的是非静态嵌套类, ...

  7. scala 学习笔记(04) OOP(上)主从构造器/私有属性/伴生对象(单例静态类)/apply方法/嵌套类

    一.主从构造器 java中构造函数没有主.从之分,只有构造器重载,但在scala中,每个类都有一个主构造器,在定义class时,如果啥也没写,默认有一个xxx()的主构造器 class Person ...

  8. 【转】C#类的分类(静态类、实例类、嵌套类、结构、简单的抽象类、简单的密封类)

    静态类 -------------------------------------------------------------------------------- 静态类就是在class关键字前 ...

  9. C++ 嵌套类使用(一)

    一.嵌套类 在一个类的内部定义另一个类,我们称之为嵌套类(nested class),或者嵌套类型.之所以引入这样一个嵌套类,往往是因为外围类需要使用嵌套类对象作为底层实现,并且该嵌套类只用于外围类的 ...

随机推荐

  1. android64位机子兼容32位.so库文件

    http://blog.csdn.net/vhawk/article/details/49964475 猴子在调用高德地图的时候,遇到一个操作系统兼容问题,异常堆栈说是找不到so库文件,猴子就不懂了, ...

  2. JAVA生成EXCEL图表

    跟据客户的要求,需要开发一套包括图形的报表,还需要导出WORD 图表需要这样: 这样: 这样: 还有这样: 接下来是实现思路: 以往用的最多的就是JFreechart,手上也有实现各种图形的资源,但是 ...

  3. Cocos2d-x中__Array容器以及实例介绍

    __Array类在Cocos2d-x 2.x时代它就是CCArray类.它是模仿Objective-C中的NSArray类而设计的,通过引用计数管理内存.__Array继承于Ref类,因此它所能容纳的 ...

  4. Sublime Text 3下 Emmet 使用小技巧

    Emmet常用技巧:(输入下面简写,按Tab键可触发效果)                          生成 HTML 文档初始结构 html:5 或者 ! 生成 HTML5 结构        ...

  5. wpf DataGrid 双击获取当前行的控件

    <DataGrid Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top& ...

  6. (转)JVM参数调优八大技巧

    这里和大家分享一下JVM参数调优的八条经验,JVM参数调优,这是很头痛的问题,设置的不好,JVM不断执行FullGC,导致整个系统变得很慢,网站停滞时间能达10秒以上,相信通过本文的学习你对JVM参数 ...

  7. css3学习笔记之边框

    CSS3 圆角 border-radius 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <!DOCTYPE html> <h ...

  8. spring aop配置及用例说明(4)

    欢迎交流转载:http://www.cnblogs.com/shizhongtao/p/3476161.html 这里简单对xml的配置方式做一下描述.代码还是上一篇(http://www.cnblo ...

  9. 编程语言中的Namespace

    Namespace 1.C struct 2.C++(Pronounced 'see jia-jia' or 'see plus-plus') namespace 3.Python module(s) ...

  10. 009.EscapeRegExChars

    类型:function 可见性:public 所在单元:RegularExpressionsCore 父类:TPerlRegEx 把转义字符变成原意字符 例如\d意为0~9某个数字,通过此函数转换后则 ...