在C++中,以关键字friend声明友元关系,友元可以访问与其有friend关系的类中的私有成员。
1、友元函数
将一个函数声明为当前类的友元函数,则该函数可以访问当前类的保护成员和私有成员。
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
class Student
{
public:
Student(float c,float e,float m)
{
chinese=c;
english=e;
math=m;
}
friend void output(Student &s);
private:
int age;
double height;
float chinese;
float english;
float math;
};
//全局函数,且是student类的友元函数
//注意该函数不属于类Student
void output(Student &s)
{
cout<<"output:"<<s.chinese<<"--"<<s.english<<"--"<<s.math<<endl;
}
int main()
{
Student s(78.5,88.2,95);
output(s);
system("pause");
return 0;
}
2、友元类
若将一个类B声明为类A的友元类,则B中的所有成员函数都是类A的友元函数,可以访问类A中的protected,private成员。
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
class Date;
class Time
{
//可以声明在该类的任何地方
//将Date类声明为Time类的友元类
//则Date类中的成员函数可以访问Time类的private成员
friend class Date;
public:
Time(int,int,int);
private:
int hour;
int min;
int sec;
};
Time::Time(int h,int m,int s)
{
hour=h;
min=m;
sec=s;
}
class Date
{
public:
Date(int,int,int);
void t_hour(Time &);
void t_min(Time &);
void t_sec(Time &);
void output(Date &,Time &);
private:
int year;
int mon;
int day;
};
Date::Date(int y,int m,int d)
{
year=y;
mon=m;
day=d;
}
void Date::t_hour(Time &t)
{
cout<<"Hour="<<t.hour<<endl;
}
void Date::t_min(Time &t)
{
cout<<"Min="<<t.min<<endl;
}
void Date::t_sec(Time &t)
{
cout<<"Second="<<t.sec<<endl;
}
void Date::output(Date &d,Time &t)
{
cout<<"current time is "<<d.year<<"-"<<d.mon<<"-"<<d.day<<" "<<t.hour<<":"<<t.min<<":"<<t.sec<<endl;
}
int main()
{
Time t(15,33,22);
Date d(2016,2,2);
d.t_hour(t);
d.t_min(t);
d.t_sec(t);
d.output(d,t);
system("pause");
return 0;
}
问题:
若类B是A的友元类,则B的成员函数可以访问A的私有成员,那么A和B是否彼此成为友元,A的成员函数也可以访问B的私有成员呢?
答案是否定的,因为要访问私有成员,首先要定义对象,而A,B中总有一个先定义,一个后定义,后定义的那个类称为前类的友元类,可以访问它的私有成员,而在定义前类时,后类的大小是未知的,即使也定义成友元,还是不能访问。这里就用到了类的提前引用声明。
3、类的提前引用声明
一般情况下,类必须先声明(给出类体),才能使用。
如果需要在类声明之前,使用该类的名字去定义指向该类对象的指针或引用,可以使用提前引用声明。
如上例所示:使用前(未定义)引用: class Date;
但是:不能因为提前引用声明,而去定义一个类的对象,这是不允许的。
因为在定义一个对象时,要为这个对象分配存储空间,在正式声明类之前,编译系统无法确定应为对象分配多大的存储空间。
只有见到类体之后才能确定,所以不能在定义类之前去定义一个类的对象。
但是可以在声明类之前,先使用该类的名字定义一个该类的指针或引用。
因为指针变量和引用本身的大小是固定的,它与指向的类对象的大小无关。