原文:http://blog.csdn.net/jackychu/article/details/3020866
通常對於普通函數來說,要訪問類的保護成員是不可能的,如果想這麼做那麼必須把類的成員都申明成爲public(共用的),然而這麼做帶來的問題便是任何外部函數都可以毫無約束的訪問它操作它,c++利用friend修飾符,可以讓一些你設定的函數能夠對這些保護數據進行操作,避免把類成員全部設置成public,最大限度的保護數據成員的安全。
友元能夠使得普通函數直接訪問類的保護數據(private:或protected: ),避免了類成員函數的頻繁調用,可以節約處理器開銷,提高程序的效率,但矛盾的是,即使是最大限度的保護,同樣也破壞了類的封裝特性,這即是友元的缺點,在現在cpu速度越來越快的今天我們並不推薦使用它,但它作爲c++一個必要的知識點,一個完整的組成部分,我們還是需要討論一下的。
1.在類裏聲明一個普通函數,在前面加上friend修飾,那麼這個函數就成了該類的友元,可以訪問該類的一切成員
下面我們來看一段代碼,看看我們是如何利用友元來訪問類的一切成員的。
#include <iostream>
using namespace std;
class Internet
{
public:
Internet(char *name,char *address)
{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
friend void ShowN(Internet &obj);//友元函數的聲明,函數ShowN(Internet &obj不是類Internet的成員函數,而是外部的函數
private: //或protected:
char name[20];
char address[20];
};
void ShowN(Internet &obj)//函數定義,不能寫成,void Internet::ShowN(Internet &obj)
{
cout<<obj.name<<endl;
}
void main()
{
Internet a("中國軟件開發實驗室","www.cndev-lab.com");
ShowN(a);
cin.get();
}
運行結果:
上面的代碼通過友元函數的定義,我們成功的訪問到了a對象的保護成員name,友元函數並不能看做是類的成員函數,它只是個被聲明爲類友元的普通函數,所以在類外部函數的定義部分不能夠寫成void Internet::ShowN(Internet &obj),這一點要注意。
2. 一個普通函數可以是多個類的友元函數
對上面的代碼我們進行修改,注意觀察變化:
#include <iostream>
using namespace std;
class Country;
class Internet
{
public:
Internet(char *name,char *address)
{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
friend void ShowN(Internet &obj,Country &cn);//注意這裏,定義了友元函數ShowN
private:
char name[20];
char address[20];
};
class Country
{
public:
Country()
{
strcpy(cname,"中國");
}
friend void ShowN(Internet &obj,Country &cn);//注意這裏,也定義了友元函數ShowN
protected:
char cname[30];
};
void ShowN(Internet &obj,Country &cn) //函數的參數分別來自兩個不同的類的對象
{
cout<<cn.cname<<"|"<<obj.name<<endl;
}
void main()
{
Internet a("中國軟件開發實驗室","www.cndev-lab.com");
Country b;
ShowN(a,b);
cin.get();
}
運行結果:
3一個類的成員函數函數也可以是另一個類的友元,從而可以使得一個類的成員函數可以操作另一個類的數據成員
我們在下面的代碼中增加一類Country,注意觀察:
#include <iostream>
using namespace std;
class Internet;
class Country
{
public:
Country()
{
strcpy(cname,"中國");
}
void Editurl(Internet &temp);//成員函數的聲明
protected:
char cname[30];
};
class Internet
{
public:
Internet(char *name,char *address)
{
strcpy(Internet::name,name); //類或命名空間引用其下的採用是用::,對象引用其成員時用.
strcpy(Internet::address,address);
}
friend void Country::Editurl(Internet &temp);//把Country類中的Editurl函數申明爲Internet類的友元函數
protected:
char name[20];
char address[20];
};
void Country::Editurl(Internet &temp)//在類的外部定義成員函數,注意定義形式:返回值 類名::函數名(參數表)
{
strcpy(temp.address,"change address by a friend fuction of another class");
cout<<temp.name<<"|"<<temp.address<<endl;
}
void main()
{
Internet a("中國軟件開發實驗室","www.cndev-lab.com");
Country b;
b.Editurl(a);
cin.get();
}
運行結果:
4.整個類也可以是另一個類的友元,該友元也可以稱做爲友類。友類的每個成員函數都可以訪問另一個類的所有成員。
示例代碼如下:
#include <iostream>
using namespace std;
class Internet;
class Country
{
public:
Country()
{
strcpy(cname,"中國");
}
friend class Internet;//將Internet類申明爲Country的友類
protected:
char cname[30];
};
class Internet
{
public:
Internet(char *name,char *address)
{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
void Editcname(Country &temp);
protected:
char name[20];
char address[20];
};
void Internet::Editcname(Country &temp) //定義Internet類的成員函數
{
strcpy(temp.cname,"保護成員被友類中的函數更改");
cout<<temp.cname;
}
void main()
{
Internet a("中國軟件開發實驗室","www.cndev-lab.com");
Country b;
a.Editcname(b);
cin.get();
}
在上面的代碼中我們成功的通過Internet類Editcname成員函數操作了Country類的保護成員cname。
運行結果:
5.使用友元類時應注意:
友元關係不能被繼承
友元關係是單向的,不具有交換性。若類B是類A的友元,類A不一定是類B的友元,要看在類中是否有相應的聲明。
友元關係不具有傳遞性。若類B是類A的友元,類C是B的友元,類C不一定是類A的友元,同樣要看類中是否有相應的申明
在編程中,我們使用友元的另外一個重要原因是爲了方便重載操作符的使用,這些內容我們將在後面的教程着重討論!