C++ 中的友元函数

在C++中,一个全局方法或者一个类,或者一个类的成会员方法被在另一个类中声明为友元,那么这个方法或类就可以访问另一个类中的私有成员.

1,一个全局方法被一个类声明为友元.

#include <iostream>
using namespace std;
//声明一个矩形
class Rect
{
private:
	int m_width;
	int m_length;
public:
	Rect(int width,int length):m_width(width),m_length(length){
	}
	int getLength(){
		return m_length;
	}
	int getWidth(){
		return m_width;
	}	
	//如果注释掉下边这行,则编译过程提示错误,
	/*   此文件名为friend.cpp
	friend.cpp: In function `int ComputerArea(Rect&)':
	friend.cpp:8: error: `int Rect::m_width' is private
	friend.cpp:24: error: within this context
	friend.cpp:9: error: `int Rect::m_length' is private
	friend.cpp:24: error: within this context
	*/
	friend int ComputerArea(Rect& rect);
};

int ComputerArea(Rect& rect)
{
	return rect.m_width*rect.m_length;
}
int main()
{
	Rect rect(2,3);
	cout << "面积等于:" << ComputerArea(rect) << endl;
	return 0;
}

2,一个类的成员方法被另一个类声明为友元

#include <iostream>
#include <cstring>
using namespace std;

class CItem;
class CList
{
private:
	CItem * m_pItem;
public:
	CList();
	~CList();
	void OutPutItem();
};

class CItem
{
friend void CList::OutPutItem();
private:
	char m_name[128];
	void OutPutName(){
		cout << m_name << endl;
	}
public:
	CItem(){memset(m_name,0,128);}
	void SetName(const char* pData){
		if(pData!=NULL){
			strcpy(m_name,pData);
		}
	}
};
//由于void CList::OutPutItem() 被声明为CItem的友元方法,此方法可以访问CItem的任何成员.
void CList::OutPutItem(){
	m_pItem->SetName("将类的成员方法声明为另一个类的友元方法,此方法可以肆无忌惮访问另一个类的数据成员");
	m_pItem->OutPutName();
}
CList::CList(){
	m_pItem=new CItem();	
}
CList::~CList(){delete m_pItem;	m_pItem=NULL;}

int main()
{
	CList list;
	list.OutPutItem();
	return 0;
}


3,一个类被声明为另一个类的友元类

#include <iostream>
using namespace std;

class Rect
{
private:
	int m_width;
	int m_length;
public:
	Rect(){}
	Rect(int width,int length):m_width(width),m_length(length){
	}
	int getLength(){
		return m_length;
	}
	int getWidth(){
		return m_width;
	}		
	friend int ComputerArea(Rect& rect);
	friend class B;
	/* 如果不声明为友元类,则会有下边的编译错误
	D:\>g++ friend.cpp -omain
	friend.cpp: In constructor `B::B()':
	friend.cpp:7: error: `int Rect::m_width' is private
	friend.cpp:34: error: within this context
	friend.cpp:8: error: `int Rect::m_length' is private
	friend.cpp:35: error: within this context
	friend.cpp: In member function `void B::PrintArea()':
	friend.cpp:7: error: `int Rect::m_width' is private
	friend.cpp:38: error: within this context
	friend.cpp:8: error: `int Rect::m_length' is private
	friend.cpp:38: error: within this context
	*/

};
int ComputerArea(Rect& rect)
{
	return rect.m_width*rect.m_length;
}

class B
{
private:
	Rect rect;
public:
	B(){
			rect.m_width=12;
			rect.m_length=12;
	}
	void PrintArea(){
		cout <<"面积等于:" << rect.m_width*rect.m_length<< endl;
	}
};

int main()
{
	Rect rect(2,3);
	cout << "面积等于:" << ComputerArea(rect) << endl;
	
	//被声明为友元类之后,该类中的方法可以肆无忌惮的操作rect类的私有成员.
	B b;
	b.PrintArea();
	return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值