詳解C++編程中的嵌套類的聲明與其中的函數(shù)使用
可以在一個(gè)類的范圍內(nèi)聲明另一個(gè)類。這樣的類稱為“嵌套類”。 嵌套類被視為在封閉類的范圍內(nèi)且可在該范圍內(nèi)使用。若要從嵌套類的即時(shí)封閉范圍之外的某個(gè)范圍引用該類,則必須使用完全限定名。
下面的示例演示如何聲明嵌套類:
// 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 類型的任何對象。
嵌套類只能從封閉類中直接使用名稱、類型名稱,靜態(tài)成員的名稱和枚舉數(shù)。若要使用其他類成員的名稱,您必須使用指針、引用或?qū)ο竺?br />
在前面的 BufferedIO 示例中,枚舉 IOError 可由嵌套類中的成員函數(shù)、BufferedIO::BufferedInput 或 BufferedIO::BufferedOutput 直接訪問,如函數(shù) good 中所示。
注意
嵌套類僅在類范圍內(nèi)聲明類型。它們不會(huì)導(dǎo)致創(chuàng)建嵌套類的包含對象。前面的示例聲明兩個(gè)嵌套類,但未聲明這些類類型的任何對象。
在將類型名稱與前向聲明一起聲明時(shí),會(huì)引發(fā)嵌套類聲明的范圍可見性的異常。在這種情況下,由前向聲明聲明的類名在封閉類的外部可見,其范圍定義為最小的封閉非類范圍。例如:
// 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;
}
嵌套類中的訪問權(quán)限
將一個(gè)類嵌入另一個(gè)類中不會(huì)為嵌入類的成員函數(shù)提供特殊訪問權(quán)限。同樣,封閉類的成員函數(shù)不具有對嵌套類的成員的特殊訪問權(quán)限。
嵌套類中的成員函數(shù)
在嵌套類中聲明的成員函數(shù)可在文件范圍中定義。前面的示例可能已編寫:
// 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 語法用于聲明函數(shù)名稱。聲明:
BufferedIO::BufferedInput::read()
表示“作為 read 類(位于 BufferedInput 類的范圍中)的成員的 BufferedIO 函數(shù)?!?由于此聲明使用 qualified-type-name 語法,因此以下形式的構(gòu)造是可能的:
typedef BufferedIO::BufferedInput BIO_INPUT; int BIO_INPUT::read()
上述聲明與前一個(gè)聲明等效,但它使用了 typedef 名稱來代替類名稱。
嵌套類中的友元函數(shù)
嵌套類中聲明的友元函數(shù)被認(rèn)為是在嵌套類而不是封閉類的范圍內(nèi)。因此,友元函數(shù)未獲得對封閉類的成員或成員函數(shù)的特定訪問權(quán)限。如果需要使用在友元函數(shù)中的嵌套類中聲明的名稱,并且友元函數(shù)是在文件范圍內(nèi)定義的,請使用限定的類型名稱,如下所示:
// 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()
{
}
以下代碼演示聲明為友元函數(shù)的函數(shù) GetExtendedErrorStatus。在文件范圍內(nèi)定義的函數(shù)中,將消息從靜態(tài)數(shù)組復(fù)制到類成員中。請注意,GetExtendedErrorStatus 的更佳實(shí)現(xiàn)是將其聲明為:
int GetExtendedErrorStatus( char *message )
利用前面的接口,許多類可以通過傳遞要復(fù)制錯(cuò)誤消息的內(nèi)存位置來使用此函數(shù)的服務(wù)。
相關(guān)文章
C++中rapidjson將map轉(zhuǎn)為json的方法
今天小編就為大家分享一篇關(guān)于C++中rapidjson將map轉(zhuǎn)為json的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04
C語言深入講解之從函數(shù)棧幀角度理解return關(guān)鍵字
在C語言中,一般情況下函數(shù)的返回值是通過函數(shù)中的return語句來實(shí)現(xiàn)的,每調(diào)用一次return語句只能從函數(shù)中返回一個(gè)值,這篇文章主要給大家介紹了關(guān)于C語言從函數(shù)棧幀角度理解return關(guān)鍵字的相關(guān)資料,需要的朋友可以參考下2021-09-09
C++實(shí)現(xiàn)OpenCV方框?yàn)V波的代碼
這篇文章主要介紹了C++ OpenCV方框?yàn)V波的實(shí)現(xiàn),方框?yàn)V波是均值濾波的一種形式,今天通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
OpenCV+Qt實(shí)現(xiàn)圖像處理操作工具的示例代碼
這篇文章主要介紹了利用OpenCV+Qt實(shí)現(xiàn)圖像處理操作工具,可以實(shí)現(xiàn)雪花屏、高斯模糊、中值濾波、毛玻璃等操作,感興趣的可以了解一下2022-08-08
C/C++代碼操作MySQL數(shù)據(jù)庫詳細(xì)步驟
這篇文章主要給大家介紹了關(guān)于C/C++代碼操作MySQL數(shù)據(jù)庫的相關(guān)資料,通過文中的這些示例,我們可以連接到MySQL數(shù)據(jù)庫,并執(zhí)行常見的數(shù)據(jù)庫操作,如創(chuàng)建表、插入數(shù)據(jù)和查詢數(shù)據(jù),需要的朋友可以參考下2023-12-12

