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),或者嵌套类型.之所以引入这样一个嵌套类,往往是因为外围类需要使用嵌套类对象作为底层实现,并且该嵌套类只用于外围类的 ...
随机推荐
- CF下Split的使用
在Compact Framework 下 String的Split不能正确的处理 字符串 分割字符串 如对字符串 "abcfdef12abcedef23" 以"ef&qu ...
- .net 下载图片
最近boss让写一个二维码的生成器,但是二维码生成后用户如果想下载二维码,这就促使我写l了 下载功能,小弟自认为技术不咋样,是个彻头彻尾的码农,本先是想用js来实现功能,但是查找了好多资料也没能实现, ...
- Jquery 模糊匹配ID
[属性名称] 匹配包含给定属性的元素[att=value] 匹配包含给定属性的元素 (大小写区分)[att*=value] 模糊匹配[att!=value] 不能是这个值[att$=value] 结尾 ...
- Cocos2d-x实例:单点触摸事件
addChild(boxC,30, kBoxC_Tag); ...
- ios开发:代理设计模式
代理是一种简单而功能强大的设计模式,这种模式用于一个对象“代表”另外一个对象去做和实现一些东西. 主对象维护一个代理(delegate)的引用并且在合适的时候向这个代理发送消息,这个消息通知“代理”对 ...
- NTT研发
2.研发标准 stead---大型机 css terasolunt---反向自动生成设计书 3.开发阶段做好设计,确定需求,测试阶段只是做产品有多差或者完成了多少需求,不是用来提高产品质量的过程
- 输入与enter
#include<iostream> using namespace std; int main() { char a,b,c; while(scanf("%c%c%c" ...
- iOS 非ARC基本内存管理系列 -手把手教你ARC——iOS/Mac开发ARC入门和使用(转)
手把手教你ARC——iOS/Mac开发ARC入门和使用 Revolution of Objective-c 本文部分实例取自iOS 5 Toturail一书中关于ARC的教程和公开内容,仅用于技术交流 ...
- RHEL6.3 ftp服务器参数的戏说——不看白不看,看了不白看
大家都知道ftp服务器的应用何止广,简直就是无处不在,配置简单的ftp服务谁都会,无外乎就是刚安装好ftp服务,一启动就ok了:或是达到简单的上传下载修改下/var/ftp/pub的权限,配合配置文件 ...
- Repeater中将int类型和bool类型的字段以字符显示出来
图一 图二 比如将图一中是否显示中的列显示以图二中的方式显示: 方法1: 1.在后台编写方法:a.aspx.cs代码如下 //IsShow字段显示的方法public string GetStrIsSh ...