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中不能编译通过,但在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++ 嵌套类使用(二)的更多相关文章
- mfc 嵌套类
嵌套类 一. 嵌套类 嵌套类的定义 将某个类的定义放在另一个类的内部,这样的类定义,叫嵌套类. class AAA { int aaa; class BBB { int bbb; //其它成员或者函数 ...
- C++学习笔记(十二):类继承、虚函数、纯虚函数、抽象类和嵌套类
类继承 在C++类继承中,一个派生类可以从一个基类派生,也可以从多个基类派生. 从一个基类派生的继承称为单继承:从多个基类派生的继承称为多继承. //单继承的定义 class B:public A { ...
- (十二)java嵌套类和内部类
嵌套类和内部类:在一个类里边定义的类叫做嵌套类,其中没有static修饰的嵌套类是我们通常说的内部类,而被static修饰的嵌套类不常用.有的地方没有嵌套类和内部类的区分,直接是嵌套类就称作内部类,没 ...
- Java 嵌套类和内部类演示样例<二>
嵌套类(nested class)是一个在还有一个类或接口内部声明的类. 嵌套类分为两种:静态内部类(static inner class)和非静态嵌套类(non-static nested clas ...
- Scala 面向对象(十二):嵌套类
在Scala中,你几乎可以在任何语法结构中内嵌任何语法结构.如在类中可以再定义一个类,这样的类是嵌套类,其他语法结构也是一样. 嵌套类类似于Java中的内部类. Scala嵌套类的使用1 请编写程序, ...
- JAVA 嵌套类和内部类
一.什么是嵌套类及内部类? 可以在一个类的内部定义另一个类,这种类称为嵌套类(nested classes),它有两种类型: 静态嵌套类和非静态嵌套类.静态嵌套类使用很少,最重要的是非静态嵌套类, ...
- scala 学习笔记(04) OOP(上)主从构造器/私有属性/伴生对象(单例静态类)/apply方法/嵌套类
一.主从构造器 java中构造函数没有主.从之分,只有构造器重载,但在scala中,每个类都有一个主构造器,在定义class时,如果啥也没写,默认有一个xxx()的主构造器 class Person ...
- 【转】C#类的分类(静态类、实例类、嵌套类、结构、简单的抽象类、简单的密封类)
静态类 -------------------------------------------------------------------------------- 静态类就是在class关键字前 ...
- C++ 嵌套类使用(一)
一.嵌套类 在一个类的内部定义另一个类,我们称之为嵌套类(nested class),或者嵌套类型.之所以引入这样一个嵌套类,往往是因为外围类需要使用嵌套类对象作为底层实现,并且该嵌套类只用于外围类的 ...
随机推荐
- Servlet之创建与配置
上篇已将介绍完了,下面来实践操作走一个: 首先在名为"com.caiduping"的包中创一个MyFilter的对象: 1 package com.caiduping; 2 3 i ...
- sqlserver之二进制和字符串sql语句
正常情况下我们对数据库的操作就是如下的写法来操作数据库 ID AS 编号,BookName AS 书名 FROM dbo.books ORDER BY ID; ; 但是在客户正在使用的数据库里,我们开 ...
- sqlserver根据id集合,批量插入。(巧用sqlserver内置函数)
场景如下,传入的id,如1,3,4,88.可以在.net后台处理,但是我更习惯在数据库中操作. 插入数据时可以这样处理,直接贴代码. CREATE PROCEDURE pro_CategorySave ...
- iOS UIView 快速修改 frame
我们修改frame中的某个值,需要进行繁琐的书写,例如: (1). 直接设置位置大小 view.frame = CGRectMake(0, 0, 320, 150); (2). 只修改某个值 view ...
- 使用Ctex总结1
使用Ctex应该是每一个做学术研究的人要学会掌握的. 它的基本结构: \documentclass[11pt,two side,a4paper]{cctart}%使用cctart可以让摘要变成中文,比 ...
- [Bootstrap]概述
——1.html,css,javascript框架 ——2.一般开发响应式布局或者移动优先的项目可以优先考虑 优点 1.css开发版本(可以直接上手)和源码版本(可根据l ...
- Script 代码段
script代码段 1.script代码段的执行 在Javascript代码中,可以使用script作为基本标识,script代码段在运行过程中是分段解析与执行的. 2.script代码段执行流程 在 ...
- Item47
STL迭代器分类:input迭代器.output迭代器.forward迭代器.bidirectional迭代器.random access迭代器. Input迭代器:只能向前移动,一次一步,客户只读取 ...
- 新贵HTML5,2016的发展方向会怎样?
2014年下半年,微信捧火了HTML5小游戏,自此国内各行各业开始对HTML5保持高涨关注.2015年是国内HTML5行业迅速发展的一年,在媒体大肆渲染“互联网寒冬”之际,HTML5作品的生产数量和传 ...
- javassist动态修改class
import java.io.IOException;import java.io.PrintWriter; import javassist.CannotCompileException;impor ...