编辑本段1.友元函数
如果在本类以外的其它地方定义了一个函数(这个函数可以是不属于任何类的非成员函数,也可以是其它类的成员函数),在类体中用friend对该函数进行声明,此函数就称为本类的友元函数。一个类的友元函数可以访问这个类中的private成员。1.1将全局函数声明为友元函数
如果要将一个全局函数(call)声明为本类(Time)的友元函数,则只需要在本类的函数声明部分声明该函数为friend。此时,该函数可以访问本类的private成员。class Time{ public: Time(int=1,int=1,int=1); friendvoid call(Time &);//声明友元函数 private: int hour; int min; int sec; }; Time::Time(int h,int m,int s){ hour=h; min=m; sec=s; } void call(Time &t) {//全局函数,且是Time类的友元函数 cout<<"Call:"<<t.hour<<"-"<<t.min<<"-"<<t.sec<<endl;//访问private成员 } int main(){ Time t; call(t); system("PAUSE"); return EXIT_SUCCESS; } |
1.2友元成员函数
如果需要将目标类(Time)中的成员函数(call)声明为本类(Date)的友元函数,则需要在本类的函数声明部分声明该函数为friend。此时,该函数可以访问本类的private成员。class Date; //对Date类的提前引用声明 class Time{ public: Time(int=1,int=1,int=1); void call(Date &);//声明成员函数 private: int hour; int min; int sec; }; class Date{ public: Date(int=1,int=1,int=2008); friendvoid Time::call(Date&); //声明Time类的call为本类的友元成员函数 private: int year; int mon; int day; }; Time::Time(int h,int m,int s){ hour=h; min=m; sec=s; } void Time::call(Date &d) { cout<<"TIME:"<<hour<<"-"<<min<<"-"<<sec<<endl; cout<<"DATE:"<<d.mon<<"-"<<d.day<<"-"<<d.year<<endl; //访问Date类的private成员 } Date::Date(int m,int d,int y){ mon=m; day=d; year=y; } int main(){ Time t; Date d; t.call(d); system("PAUSE"); return EXIT_SUCCESS; } |
1.3关于类的提前引用声明
一般情况下,类必须先声明(给出类体),才能使用。如果需要在类声明之前,使用该类的名字去定义指向该类对象的指针或引用,可以使用提前引用声明。如上例所示,class Date; //对Date类的提前引用声明 … void call(Date &);//Date类的引用 … class Date{…}//Date类的声明 |
class Date; //紧接着马上定义一个Date类的对象 Date d1;error:aggregate `Date d1‘ has incomplete type and cannot be defined … class Date{…} |
1.4将一个函数声明为多个类的友元函数
在这种情况下,该函数可以同时访问多个类的private成员。class Date; //对Date类的提前引用声明 class Time{ public: Time(int=1,int=1,int=1); friendvoid call(Time&,Date&);//声明函数call为本类的友元成员函数 private: int hour; int min; int sec; }; class Date{ public: Date(int=1,int=1,int=2008); friendvoid call(Time&,Date&); //声明函数call为本类的友元成员函数 private: int year; int mon; int day; }; Time::Time(int h,int m,int s){ hour=h; min=m; sec=s; } Date::Date(int m,int d,int y){ mon=m; day=d; year=y; } void call(Time &t,Date &d) { cout<<"TIME:"<<t.hour<<"-"<<t.min<<"-"<<t.sec<<endl; cout<<"DATE:"<<d.mon<<"-"<<d.day<<"-"<<d.year<<endl; } int main(){ Time t; Date d; call(t,d); system("PAUSE"); return EXIT_SUCCESS; } |
编辑本段2.友元类
可以将一个类(B)声明为当前类(A)的友元。此时,当前类(A)的友元类(B)中的所有成员函数都是当前类的友元函数,可以访问当前类的private成员。class Date; //对Date类的提前引用声明 class Time{ public: Time(int=1,int=1,int=1); friendclass Date;//将Date类声明为当前类的友元类 private: int hour; int min; int sec; }; class Date{ public: Date(int=1,int=1,int=2008); void call_hour(Time&); void call_min(Time&); void call_sec(Time&); private: int year; int mon; int day; }; Time::Time(int h,int m,int s){ hour=h; min=m; sec=s; } Date::Date(int m,int d,int y){ mon=m; day=d; year=y; } void Date::call_hour(Time &t){ cout<<"HOUR:"<<t.hour<<endl; } void Date::call_min(Time &t){ cout<<"MINUTE:"<<t.min<<endl; } void Date::call_sec(Time &t){ cout<<"SECOND:"<<t.sec<<endl; } int main(){ Time t; Date d; d.call_hour(t); d.call_min(t); d.call_sec(t); system("PAUSE"); return EXIT_SUCCESS; } |