用途1:通过 friend class 申明, 限定class的使用范围
// experiment_FriendClass.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "experiment_FriendClass.h" #include "ClassA.h" #include "ClassB.h" #include "ClassC.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // The one and only application object CWinApp theApp; using namespace std; void fnTestFriendClass(void); int _tmain(int argc, TCHAR * argv[], TCHAR * envp[]) { int nRetCode = 0; // initialize MFC and print and error on failure if(!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs cerr << _T("Fatal Error: MFC initialization failed") << endl; nRetCode = 1; } else { // TODO: code your application's behavior here. CString strHello; strHello.LoadString(IDS_HELLO); cout << (LPCTSTR)strHello << endl; fnTestFriendClass(); } getchar(); return nRetCode; } void fnTestFriendClass(void) { /** * classB, classC 的构造函数和析构函数都是private * 通过 friend class 申明, 限定了classB, classC的使用范围 * 保证了classB 只能在 classA 中使用 * 保证了classC 只能在 classB 中使用 */ CClassA classA; /** * error C2248: 'CClassB::CClassB' : cannot access private member declared in class 'CClassB' */ //CClassB classB; /** * error C2248: 'CClassC::CClassC' : cannot access private member declared in class 'CClassC' */ //CClassC classC; classA.HelloToMyFriend(); /** run results i'm CClassA, my friend is CClassB I'm u friend, my name is "ClassB" my friend is CClassC I'm u friend, my name is "ClassC" my friend only u */ }
// ClassA.h: interface for the CClassA class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_CLASSA_H__89BD7B07_D219_4FA7_AB2C_28BF08F24C61__INCLUDED_) #define AFX_CLASSA_H__89BD7B07_D219_4FA7_AB2C_28BF08F24C61__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "ClassB.h" #include "ClassC.h" class CClassA { public: CClassA(); virtual ~CClassA(); public: void HelloToMyFriend(); private: CClassB m_classB; }; #endif // !defined(AFX_CLASSA_H__89BD7B07_D219_4FA7_AB2C_28BF08F24C61__INCLUDED_)
// ClassB.h: interface for the CClassB class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_CLASSB_H__2C633207_965D_4165_BBA5_E5F688161FC9__INCLUDED_) #define AFX_CLASSB_H__2C633207_965D_4165_BBA5_E5F688161FC9__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "ClassC.h" class CClassB { private: CClassB(); virtual ~CClassB(); public: void HelloToMyFriend(); private: CClassC m_classC; /** * CClassB的构造析构函数声明为私有的 * 通过friend class声明, 使CClassB只被CClassA使用 */ friend class CClassA; }; #endif // !defined(AFX_CLASSB_H__2C633207_965D_4165_BBA5_E5F688161FC9__INCLUDED_)
// ClassC.h: interface for the CClassC class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_CLASSC_H__256D5057_179C_4B46_8EA2_A87ED6B49D97__INCLUDED_) #define AFX_CLASSC_H__256D5057_179C_4B46_8EA2_A87ED6B49D97__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CClassC { private: CClassC(); virtual ~CClassC(); public: void HelloToMyFriend(); private: /** * CClassB的构造析构函数声明为私有的 * 通过friend class声明, 使CClassC只被CClassB使用 */ friend class CClassB; }; #endif // !defined(AFX_CLASSC_H__256D5057_179C_4B46_8EA2_A87ED6B49D97__INCLUDED_)
// ClassA.cpp: implementation of the CClassA class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "experiment_FriendClass.h" #include "ClassA.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[] = __FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CClassA::CClassA() { } CClassA::~CClassA() { } void CClassA::HelloToMyFriend() { printf("/ni'm CClassA, my friend is CClassB/n/n"); m_classB.HelloToMyFriend(); }
// ClassB.cpp: implementation of the CClassB class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "experiment_FriendClass.h" #include "ClassB.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[] = __FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CClassB::CClassB() { } CClassB::~CClassB() { } void CClassB::HelloToMyFriend() { printf("I'm u friend, my name is /"ClassB/"/n"); printf("my friend is CClassC/n/n"); m_classC.HelloToMyFriend(); }
// ClassC.cpp: implementation of the CClassC class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "experiment_FriendClass.h" #include "ClassC.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[] = __FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CClassC::CClassC() { } CClassC::~CClassC() { } void CClassC::HelloToMyFriend() { printf("I'm u friend, my name is /"ClassC/"/n"); printf("my friend only u/n"); }
<2011_0918>
如果编译错误, 需要在class.h中互相申明为友元类才行.