友元提供了一种突破封装的方式,有时提供了便利。但是友元会增加耦合度,破坏了封装,所以友元不宜多用。
友元分为:
友元函数
和
友元类
友元关键字:friend
1
友元函数
问题:现在尝试去重载
operator<<
,然后发现没办法将
operator<<
重载成成员函数。
因为
cout
的输出流对
象和隐含的
this
指针在抢占第一个参数的位置
。
this
指针默认是第一个参数也就是左操作数了。但是实际使用中cout
需要是第一个形参对象,才能正常使用。所以要将
operator<<
重载成全局函数。但又会导致类外没办法访问成员,此时就需要友元来解决。operator>>
同理。
#include<iostream>
#include <ostream>
#include <istream>
using namespace std;
class Date{
public:
Date(int year=1900, int month=1, int day=1)
: _year(year)
, _month(month)
, _day(day)
{}
// d1 << cout; -> d1.operator<<(&d1, cout); 不符合常规调用
// 因为成员函数第一个参数一定是隐藏的this,所以d1必须放在<<的左侧
ostream& operator<<(ostream& _cout)
{
_cout << _year << "-" << _month << "-" << _day << endl;
return _cout;
}
istream& operator>>(istream& _cin) {
_cin >> _year;
_cin >> _month;
_cin >> _day;
return _cin;
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d;
d>>cin;
d<<cout;
}
友元函数
可以
直接访问
类的
私有
成员,它是
定义在类外部
的
普通函数
,不属于任何类,但需要在类的内部声明,声明时需要加friend
关键字。我们可以把上面的东西改成下面的代码。友元关键字friend。也就是说函数声明写在类里面,定义写在类外面。
#include<iostream>
#include <ostream>
#include <istream>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{}
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& _cout, const Date& d) {
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
istream& operator>>(istream& _cin, Date& d) {
_cin >> d._year;
_cin >> d._month;
_cin >> d._day;
return _cin;
}
int main()
{
Date d;
cin >> d;
cout << d << endl;
return 0;
}
说明
:
友元函数
可访问类的私有和保护成员,但
不是类的成员函数
友元函数
不能用
const
修饰
友元函数
可以在类定义的任何地方声明,
不受类访问限定符限制
一个函数可以是多个类的友元函数
友元函数的调用与普通函数的调用原理相同
2
友元类
友元类的所有成员函数都可以是另一个类的友元函数,都可以访问另一个类中的非公有成员。
友元关系是单向的,不具有交换性。
比如上述
Time
类和
Date
类,在
Time
类中声明
Date
类为其友元类,那么可以在
Date
类中直接访问
Time类的私有成员变量,但想在Time
类中访问
Date
类中私有的成员变量则不行。
友元关系不能传递
如果
B
是
A
的友元,
C
是
B
的友元,则不能说明
C
时
A
的友元。
友元关系不能继承,在继承位置再给大家详细介绍。
class Time
{
friend class Date; // 声明日期类为时间类的友元类,则在日期类中就直接访问Time类中的私有成
员变量
public:
Time(int hour = 0, int minute = 0, int second = 0)
: _hour(hour)
, _minute(minute)
, _second(second)
{}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{}
void SetTimeOfDate(int hour, int minute, int second)
{
// 直接访问时间类私有的成员变量
_t._hour = hour;
_t._minute = minute;
_t._second = second;
}
private:
int _year;
int _month;
int _day;
Time _t;
};
3.
内部类
概念:
如果一个类定义在另一个类的内部,这个内部类就叫做内部类
。内部类是一个独立的类,它不属于外部类,更不能通过外部类的对象去访问内部类的成员。外部类对内部类没有任何优越的访问权限。
注意:
内部类就是外部类的友元类
,参见友元类的定义,内部类可以通过外部类的对象参数来访问外部类中的所有成员。但是外部类不是内部类的友元。
特性:
1.
内部类可以定义在外部类的
public
、
protected
、
private
都是可以的。
2.
注意内部类可以直接访问外部类中的
static
成员,不需要外部类的对象
/
类名。
3. sizeof(
外部类
)=
外部类,和内部类没有任何关系。
#include<iostream>
using namespace std;
class A {
private:
static int k;
int h;
public:
class B // B天生就是A的友元
{
public:
void foo(const A& a)
{
cout << k << endl;//OK
cout << a.h << endl;//OK
}
};
};
int A::k = 1;
int main()
{
A::B b;
b.foo(A());
return 0;
}
下面讲匿名对象,直接上代码
#include<iostream>
using namespace std;
class A
{
public:
A(int a = 0)
:_a(a)
{
cout << "A(int a)" << endl;
}
~A()
{
cout << "~A()" << endl;
}
private:
int _a;
};
class Solution {
public:
int Sum_Solution(int n) {
cout << "Sum_Solution" << endl;
//...
return n;
}
};
int main()
{
A aa(1); // 有名对象 -- 生命周期在当前函数局部域
A(2); // 匿名对象 -- 生命周期在当前行
Solution sl;
sl.Sum_Solution(10);
Solution().Sum_Solution(20);//这里是调用默认构造函数,不用传参,但是必须有括号,
//不然 Solution.Sum_Solution(20);是直接用类,没有实际对象来使用函数,这是不可以的
//A& ra = A(1); // 匿名对象具有常性
const A& ra = A(1); // const引用延长匿名对象的生命周期,生命周期在当前函数局部域
A(10);
Solution().Sum_Solution(20);
}