C++中的友元和内部类

友元

友元分为:友元函数和友元类

友元提供了一种突破封装的方式,有时提供了便利。但是友元会增加耦合度,破坏了封装,所以友元不宜多用。

友元函数

问题:现在我们尝试去重载operator<<,然后发现我们没办法将operator<<重载成成员函数。因为cout的输出流对象和隐含的this指针在抢占第一个参数的位置。this指针默认是第一个参数也就是左操作数了。但是实际使用中cout需要是第一个形参对象,才能正常使用。所以我们要将operator<<重载成全局函数。但是这样的话,又会导致类外没办法访问成员,那么这里就需要友元来解决。operator>>同理。

class Date
{
public:
 	Date(int year, int month, int day)
 		: _year(year)
 		, _month(month)
 		, _day(day)
	{}
 
 	ostream& operator<<(ostream& _cout)
 	{
 		_cout<<d._year<<"-"<<d._month<<"-"<<d._day;
 		return _cout;
 	}
 
prvate:
 	int _year;
 	int _month;
 	int _day
};
int main()
{
 	Date d(2017, 12, 24);
 	d<<cout;
 	return 0; 
}

友元函数可以直接访问类的私有成员,它是定义在类外部的普通函数,不属于任何类,但需要在类的内部声明,声明时需要加friend关键字。

#include<iostream>

using namespace std;
class Data
{
	friend ostream& operator<<(ostream& _cout, const Data& d);
	friend istream& operator>>(ostream& _cin, const Data& d);
	public:
 		Data(int year, int month, int day)
 			: _year(year)
 			, _month(month)
 			, _day(day)
		{} 
	private:
 		int _year;
 		int _month;
 		int _day;
};
ostream& operator<<(ostream& _cout, const Data& d){
	_cout << d._year << "-" << d._month << "-" << d._day;
	
	return _cout;
}
istream& operator>>(istream& _cin, const Data& d){
	_cin>>d._year;
	_cin>>d._month;
	_cin>>d._day;
	return _cin;
}
int main()
{
 	Data d;
 	cin >> d;
 	cout << d <<endl;
 	return 0; 
}

说明:

  • 友元函数可访问类的私有成员,但不是类的成员函数
  • 友元函数不能用const修饰
  • 友元函数可以在类定义的任何地方声明,不受类访问限定符限制
  • 一个函数可以是多个类的友元函数
  • 友元函数的调用与普通函数的调用和原理相同

友元类

友元类的所有成员函数都可以是另一个类的友元函数,都可以访问另一个类中的非公有成员。

  • 友元关系是单向的,不具有交换性。
    比如上述Time类和Date类,在Time类中声明Date类为其友元类,那么可以在Date类中直接访问Time
    类的私有成员变量,但想在Time类中访问Date类中私有的成员变量则不行。
  • 友元关系不能传递
    如果B是A的友元,C是B的友元,则不能说明C时A的友元。
#include<iostream>
using namespace std;
class Date;
class Time{
	friend class Date; // 声明日期类为时间类的友元类,则在日期类中就直接访问Time类中的私有成员变量
	public:
		Time(int hour, int minute, int second)
			:_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;
		}
		void DisplayDate(){
			cout << _t._hour << endl;
			cout << _t._minute << endl;
			cout << _t.second << endl;
		}
	private:
		int _year;
		int _month;
		int _day;
		Time _t;
};

内部类

概念及特性

概念: 如果一个类定义在另一个类的内部,这个内部类就叫做内部类。注意此时这个内部类是一个独立的类,它不属于外部类,更不能通过外部类的对象去调用内部类。外部类对内部类没有任何优越的访问权限。

注意: 内部类就是外部类的友元类。注意友元类的定义,内部类可以通过外部类的对象参数来访问外部类中的所有成员。但是外部类不是内部类的友元。

特性:

  1. 内部类可以定义在外部类的public、protected、private都是可以的。
  2. 注意内部类可以直接访问外部类中的static、枚举成员,不需要外部类的对象/类名。
  3. sizeof(外部类)=外部类,和内部类没有任何关系。
#include<iostream>
#include<cstdlib>
using namespace std;
class A{
	private:
		static int k;
		int h;
	public:
		class B{
			public:
				void foo(const A& a){
					cout << k << endl;
					cout << a.h << endl;
				}
		};
};

int A::k = 1;

int main(){
	A::B b;
	b.foo(A());
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值