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. centos6.5下逻辑卷操作

    1.将两块独立磁盘分别分区 2.创建物理卷-pvcreate 3.创建卷组 4.创建逻辑卷 5.格式化逻辑卷 6.扩展逻辑卷 7.缩小逻辑卷

  2. T-SQL 使用链接库向mysql导数据遇到的奇葩事件一

    mysql表结构有 主键 非自增 text longtext类型字段多个 步骤 1.在T-SQL 临时表中处理好所有需要的字段 2.执行openquery语句 字段顺序完全按照mysql字段顺序插入 ...

  3. des算法的C#实现

    DES是Data Encryption Standard(数据加密标准)的缩写.它是一种通过56位密钥来加密64位数据的方法. public class EncryptUtility{    #reg ...

  4. 微软 Visual Studio 14 CTP2 发布

    对于在微软阵营下进行工作的团队来说,拥有最新版本的 Visual Studio 是提高效率最佳的选择,没有之一. 在本文中,我们就上个月发布的 Visual Studio "14" ...

  5. Android内存机制分析1——了解Android堆和栈

    //----------------------------------------------------------------------------------- Android内存机制分析1 ...

  6. jQuery-ui treegird 使用

    在实际应用中可能会碰到不同的需求,比如会根据每行不同的参数或属性设置来设置同列不同的editor类型,这时原有的例子就显的有点太过简单,不能实现我们的需求,现在应用我在项目中的操作为例,显示下实现同列 ...

  7. jvmstat监控jvm内存

    1.下载jvmstat-3_0.zip: 2.配置环境变量JVMSTAT_JAVA_HOME为jdk目录E:\Program Files\Java\jdk1.5.0_12 3.监控本机:  jps查看 ...

  8. 【leetcode】352. Data Stream as Disjoint Intervals

    问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...

  9. 《APUE》第五章笔记

    第五章具体介绍了标准I/O库的各种细节,要是一一列出来,有费精力且可能列不全,故只讲平常多用到的.标准输入输出是由一大批函数组成的. 要记住,标准输入输出是有缓冲的,就是当缓冲区的数据满了的时候,才会 ...

  10. discuz管理中心无法登陆

    检查下配置文件,当前管理是不是创始人. 如是,那试下修改数据库,在数据表出错时也会这样,还有一个也同时试下 \config\config_global.php 文件 $_config['admincp ...