详解C++编程中的嵌套类的声明与其中的函数使用

2025-05-27 0 30

可以在一个类的范围内声明另一个类。这样的类称为“嵌套类”。 嵌套类被视为在封闭类的范围内且可在该范围内使用。若要从嵌套类的即时封闭范围之外的某个范围引用该类,则必须使用完全限定名。
下面的示例演示如何声明嵌套类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29
// 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()

{

}

在 BufferedIO::BufferedInput 中声明 BufferedIO::BufferedOutput 和 BufferedIO。这些类名称在类 BufferedIO 的范围外不可见。但是,BufferedIO 类型的对象不包含 BufferedInput 或 BufferedOutput 类型的任何对象。
嵌套类只能从封闭类中直接使用名称、类型名称,静态成员的名称和枚举数。若要使用其他类成员的名称,您必须使用指针、引用或对象名。
在前面的 BufferedIO 示例中,枚举 IOError 可由嵌套类中的成员函数、BufferedIO::BufferedInput 或 BufferedIO::BufferedOutput 直接访问,如函数 good 中所示。
注意
嵌套类仅在类范围内声明类型。它们不会导致创建嵌套类的包含对象。前面的示例声明两个嵌套类,但未声明这些类类型的任何对象。
在将类型名称与前向声明一起声明时,会引发嵌套类声明的范围可见性的异常。在这种情况下,由前向声明声明的类名在封闭类的外部可见,其范围定义为最小的封闭非类范围。例如:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22
// 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;

}

嵌套类中的访问权限
将一个类嵌入另一个类中不会为嵌入类的成员函数提供特殊访问权限。同样,封闭类的成员函数不具有对嵌套类的成员的特殊访问权限。
嵌套类中的成员函数
嵌套类中声明的成员函数可在文件范围中定义。前面的示例可能已编写:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33
// 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()

{

}

在前面的示例中,qualified-type-name 语法用于声明函数名称。声明:

?

1
BufferedIO::BufferedInput::read()

表示“作为 read 类(位于 BufferedInput 类的范围中)的成员的 BufferedIO 函数。” 由于此声明使用 qualified-type-name 语法,因此以下形式的构造是可能的:

?

1

2

3
typedef BufferedIO::BufferedInput BIO_INPUT;

int BIO_INPUT::read()

上述声明与前一个声明等效,但它使用了 typedef 名称来代替类名称。
嵌套类中的友元函数
嵌套类中声明的友元函数被认为是在嵌套类而不是封闭类的范围内。因此,友元函数未获得对封闭类的成员或成员函数的特定访问权限。如果需要使用在友元函数中的嵌套类中声明的名称,并且友元函数是在文件范围内定义的,请使用限定的类型名称,如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41
// 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()

{

}

以下代码演示声明为友元函数的函数 GetExtendedErrorStatus。在文件范围内定义的函数中,将消息从静态数组复制到类成员中。请注意,GetExtendedErrorStatus 的更佳实现是将其声明为:

?

1
int GetExtendedErrorStatus( char *message )

利用前面的接口,许多类可以通过传递要复制错误消息的内存位置来使用此函数的服务。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解C++编程中的嵌套类的声明与其中的函数使用 https://www.kuaiidc.com/75264.html

相关文章

发表评论
暂无评论