C++_详解C++编程中的嵌套类的声明与其中的函数使用,可以在一个类的范围内声明另
嵌套类中的访问权限
嵌套类只能从封闭类中直接使用名称、类型名称,静态成员的名称和枚举数。若要使用其他类成员的名称,您必须使用指针、引用或对象名。
利用前面的接口,许多类可以通过传递要复制错误消息的内存位置来使用此函数的服务。
// member_functions_in_nested_classes.cpp class BufferedIO { public: enum IOError { None, Access, General }; class BufferedInput { public: int read(); // Declare but do not define member int good(); // functions read and good. private: IOError _inputerror; }; class BufferedOutput { // Member list. }; }; // Define member functions read and good in // file scope. int BufferedIO::BufferedInput::read() { return(1); } int BufferedIO::BufferedInput::good() { return _inputerror == None; } int main() { }
可以在一个类的范围内声明另一个类。这样的类称为“嵌套类”。 嵌套类被视为在封闭类的范围内且可在该范围内使用。若要从嵌套类的即时封闭范围之外的某个范围引用该类,则必须使用完全限定名。 表示“作为 read 类(位于 BufferedInput 类的范围中)的成员的 BufferedIO 函数。” 由于此声明使用 qualified-type-name 语法,因此以下形式的构造是可能的:
// nested_class_declarations.cpp
class BufferedIO
{
public:
enum IOError { None, Access, General };
// Declare nested class BufferedInput.
class BufferedInput
{
public:
int read();
int good()
{
return _inputerror == None;
}
private:
IOError _inputerror;
};
// Declare nested class BufferedOutput.
class BufferedOutput
{
// Member list
};
};
int main()
{
}
以下代码演示声明为友元函数的函数 GetExtendedErrorStatus。在文件范围内定义的函数中,将消息从静态数组复制到类成员中。请注意,GetExtendedErrorStatus 的更佳实现是将其声明为:
int GetExtendedErrorStatus( char *message )
在 BufferedIO::BufferedInput 中声明 BufferedIO::BufferedOutput 和 BufferedIO。这些类名称在类 BufferedIO 的范围外不可见。但是,BufferedIO 类型的对象不包含 BufferedInput 或 BufferedOutput 类型的任何对象。
typedef BufferedIO::BufferedInput BIO_INPUT;
int BIO_INPUT::read()
在嵌套类中声明的成员函数可在文件范围中定义。前面的示例可能已编写:
在前面的 BufferedIO 示例中,枚举 IOError 可由嵌套类中的成员函数、BufferedIO::BufferedInput 或 BufferedIO::BufferedOutput 直接访问,如函数 good 中所示。
下面的示例演示如何声明嵌套类:
嵌套类中的友元函数
注意
嵌套类中声明的友元函数被认为是在嵌套类而不是封闭类的范围内。因此,友元函数未获得对封闭类的成员或成员函数的特定访问权限。如果需要使用在友元函数中的嵌套类中声明的名称,并且友元函数是在文件范围内定义的,请使用限定的类型名称,如下所示:
BufferedIO::BufferedInput::read()
上述声明与前一个声明等效,但它使用了 typedef 名称来代替类名称。
嵌套类中的成员函数
将一个类嵌入另一个类中不会为嵌入类的成员函数提供特殊访问权限。同样,封闭类的成员函数不具有对嵌套类的成员的特殊访问权限。
// friend_functions_and_nested_classes.cpp #include <string.h> enum { sizeOfMessage = 255 }; char *rgszMessage[sizeOfMessage]; class BufferedIO { public: class BufferedInput { public: friend int GetExtendedErrorStatus(); static char *message; static int messageSize; int iMsgNo; }; }; char *BufferedIO::BufferedInput::message; int BufferedIO::BufferedInput::messageSize; int GetExtendedErrorStatus() { int iMsgNo = 1; // assign arbitrary value as message number strcpy_s( BufferedIO::BufferedInput::message, BufferedIO::BufferedInput::messageSize, rgszMessage[iMsgNo] ); return iMsgNo; } int main() { }
在前面的示例中,qualified-type-name 语法用于声明函数名称。声明:
// nested_class_declarations_2.cpp class C { public: typedef class U u_t; // class U visible outside class C scope typedef class V {} v_t; // class V not visible outside class C }; int main() { // okay, forward declaration used above so file scope is used U* pu; // error, type name only exists in class C scope u_t* pu2; // C2065 // error, class defined above so class C scope V* pv; // C2065 // okay, fully qualified name C::V* pv2; }
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/cjj/6145.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
只需要在调用Ctrl+B编译后
时间:2021-01-13
-
OpenGL超级宝典visual studio
时间:2021-01-04
-
Directx11 教程(2) 基本的wi
时间:2021-01-04
-
LeetCode11ContainerWithMostWate
时间:2021-01-04
-
C语言简单IT之家速成
时间:2020-12-27
-
三分钟了解Activity工作流
时间:2020-12-27
-
编译器是如何实现32位整型
时间:2020-12-27
-
C++中lower_bound函数和upper
时间:2020-12-27
热门文章
-
LeetCode11ContainerWithMostWater(最大水容器)
时间:2021-01-04
-
C语言简单编程速成
时间:2020-12-23
-
都2020了,这五个最佳C++的IDE你还没用过?
时间:2020-12-23
-
C语言源程序文件的后缀是什么?
时间:2020-12-23
-
OpenGL超级宝典visual studio 2013开发环境配置
时间:2021-01-04
-
编译器是如何实现32位整型的常量整数除
时间:2020-12-27
-
libusbwin32学习笔记(二)
时间:2020-12-27
-
C语言简单IT之家速成
时间:2020-12-27
-
C语言和Python语言有什么区别呢?
时间:2020-12-24
-
C++对象模型之RTTI的实现原理
时间:2020-12-23
