friend class

来源:互联网 发布:名侦探柯南黑衣人知乎 编辑:程序博客网 时间:2024/05/16 00:51

friend class
编辑

目录

1友元函数

1.1将全局函数声明为友元函数
1.2友元成员函数
1.3关于类的提前引用声明
1.4将一个函数声明为多个类的友元函数

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;
}
0 0
原创粉丝点击