友元函数 友元类

friend class




1.友元函数
2.友元类


  友元函数与友元类。
  C++中以关键字friend声明友元关系。友元可以访问与其有friend关系的 类中的私有成员。友元包括友元函数和友元类。


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; 
}






注意


[1]友元的关系是单向的。如果声明类B是类A的友元类,则类B中的成员函数可以访问类A中的private成员,但类A中的成员函数不能访问类B中的private成员。


[2]友元的关系不能传递。如果类B是类A的友元类,类C是类B的友元类,不等于类C是类A的友元类。


Remark:关于谁在前面的问题,就是在声明变量的时候需要对变量进行instantiation,也就是说,对于pointer和reference来说,只需要定义本身的内存地址就好,并没有对类进行解析。而定义class object的时候则需要整个类的实体,也就是defination


故而在使用类的时候,必须要看到类的实体,也就是说看到类的整个概况。并不需要整个实现。故而,仅仅的class xxx是不行的。也就是使用真个类的declareation。


不管是friend还是derived。但是对于friend类,可以知道,必须让被friend的类前面有定义。但对于friend function 则必须把引用该类的函数放在后面。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值