C++嵌套类
1、 嵌套类的名字只在外围类可见。
2、 类的私有成员只有类的成员和友元可以访问,因此外围类不可以访问嵌套类的私有成员。嵌套类可以访问外围类的成员(通过对象、指针或者引用)。
3、 一个好的嵌套类设计:嵌套类应该设成私有。嵌套类的成员和方法可以设为 public 。
4、 嵌套类可以直接访问外围类的静态成员、类型名( typedef )、枚举值。
// qiantaolei.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
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;
&nb

本文介绍了C++中的嵌套类特性,包括嵌套类的访问权限、静态成员的使用以及不同编译器对私有成员访问的处理差异。示例代码展示了嵌套类如何直接访问外围类的静态成员和私有成员。
最低0.47元/天 解锁文章
1196

被折叠的 条评论
为什么被折叠?



