【C++】-- 友元

目录

一、为什么要使用友元

二、友元函数

1.友元函数定义 

2.友元函数特性

三、友元类

1.友元类定义

2.友元类特性

四、内部类

1.内部类定义

2.内部类特性

五、总结


一、为什么要使用友元

对于自定义类型Date类,为了打印Date类的对象,需要我们自己在Date类中写打印函数:

void Print() const
{
	cout << _year << "-" << _month << "-" << _day << endl;
}

而内置类型在不写打印重载函数的情况下却能够直接打印:

#include<iostream>
using namespace std;

int main()
{
	int a = 0;
	cout << a << endl;
	return 0;
}

这是因为库里面对内置类型写了许多运算符重载函数,包括operator>>和operator<<并且能够自动识别类型构成重载,所以内置类型直接能调打印。而自定义类型需要自己写打印函数。

假如库里面对内置类型没有写operator>>和operator<<重载,对于自定义的Date类那就只有在类中自己写operator>>和operator<<重载函数了:

#include<iostream>
using namespace std;

class Date
{
public:
	//构造函数
	Date(int year = 2022, int month = 4, int day = 8)
	{
		_year = year;
		_month = month;
		_day = day;
	}
    
    //void Date::Print() const
    //{
    //	cout << _year << "-" << _month << "-" << _day << endl;
    //}
    
	void operator<<(ostream& out)
	{
		out << _year << "-" << _month << "-" << _day << endl;
	}

	void operator>>(istream& in)
	{
		in>>_year;
		in >> _month;
		in >> _day;
	}

private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	cout << d1;

	return 0;
}

但是用cout<<d1却调用不了<<运算符:

我们需要的是cout<<d1,但是调用operator<<实际上执行的是:

void operator<<(ostream& out) //void operator<<(Date* this, ostream& out)

d1作为this就变成了第一个参数,main函数里面的调用应该是d1<<cout,编译通过:

int main()
{
	Date d1;
	d1 << cout;//我们需要cout<<d1

	return 0;
}

但是d1<<cout的调用不符合我们的习惯,可读性不高,我们需要cout作为第一个参数,这样就会造成d1和cout抢占第一个参数的问题。假如把operator>>和operator<<重载不写在类里面,而是写成全局函数写在Date类外面,我们可以指定参数顺序:

#include<iostream>
using namespace std;

class Date
{
public:
	//构造函数
	Date(int year = 2022, int month = 4, int day = 8)
	{
		_year = year;
		_month = month;
		_day = day;
	}

private:
	int _year;
	int _month;
	int _day;
};

void operator<<(ostream& out,const Date& d)
{
	out << _year << "-" << _month << "-" << _day << endl;
}

void operator>>(istream& in, const Date& d)
{
	in >> _year;
	in >> _month;
	in >> _day;
}

int main()
{
	Date d1;
	cout << d1;

	return 0;
}

 但会带来访问不了私有成员变量的问题:

如何能够访问到类的私有成员变量,突破类域呢?-------使用友元,友元提供了一种突破封装的方式。友元分为友元函数和友元类。

二、友元函数

1.友元函数定义 

友元函数是定义在类外部的普通函数,不属于任何类,但可以直接访问类的私有成员,需要在类的内部使用friend关键字进行声明,类就会把友元函数当作类里面的成员。

#include<iostream>
using namespace std;

class Date
{
    //友元函数
    friend ostream& operator<<(ostream& out, const Date& d);
    friend istream& operator>>(istream& in, Date& d);

public:
	//构造函数
	Date(int year = 2022, int month = 4, int day = 8)
	{
		_year = year;
		_month = month;
		_day = day;
	}

private:
	int _year;
	int _month;
	int _day;
};

ostream& operator<<(ostream& out,const Date& d)
{
	out << d._year << "-" << d._month << "-" << d._day << endl;
	
	return _cout;
}

istream& operator>>(istream& in, Date& d)
{
	in >> d._year;
	in >> d._month;
	in >> d._day;

	return in;
}

int main()
{
	Date d1;
	cin >> d1;
	cout << d1 <<endl;

	return 0;
}

2.友元函数特性

(1)友元函数可访问类的私有和保护成员,但不是类的成员函数

(2)友元函数不能用const修饰(const修饰this指针指向的内容,友元函数作为全局函数没有this指针)

(3)友元函数可以在类定义的任何地方声明,不受类访问限定符限制,既可以声明为公有,也可以声明为私有

(4)一个函数可以是多个类的友元函数

(5)友元函数的调用与普通函数的调用和原理相同

三、友元类

1.友元类定义

友元类的所有成员函数都可以是另一个类的友元函数,都可以访问另一个类中的非公有成员。如下Date类是Time类的友元类,Date类要访问Time类,就要把Date类定义成Time类的友元:

class Date; // 前置声明
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;
};

int main()
{
	return 0;
}

2.友元类特性

(1)友元关系是单向的,不具有交换性。

在Time类中,声明Date 类是其友元类,可以在Date类中直接访问Time类的私有成员。但不能在Time中访问Date类中的私有成员。

(2)友元关系不能传递

A是B的友元,B是C的友元,但A不是C的友元。

四、内部类

1.内部类定义

一个类定义在另一个类的内部,这个内部类就叫做内部类。

内部类和外部类关系: 

(1)内部类不属于外部类,更不能通过外部类的对象调用内部类。外部类对内部类没有任何优越的访问权限。

(2)内部类是外部类的友元类,内部类可以通过外部类的对象参数来访问外部类的所有成员,但外部类不是内部类的友元。

#include<iostream>
using namespace std;

class A {
private:
	static int k;
	int h;
public:
	class B  //B是A的内部类,B是A的友元类
	{
	public:
		void foo(const A& a)
		{
			cout << k << endl;//内部类可以访问外部类的非公有成员
			cout << a.h << endl;//普通成员只能通过对象访问,不能通过类名访问
		}

	private:
		int _b;
	};
};

int A::k = 1;

int main()
{
	A::B b; //定义一个内部类对象
	b.foo(A());

	return 0;
}

2.内部类特性

(1)内部类可以定义在外部类的public、protected、private都是可以的。

(2)注意内部类可以直接访问外部类中的static、枚举成员,不需要外部类的对象/类名。

(3)sizeof(外部类)=外部类,和内部类没有任何关系

int main()
{
	A::B b;
	b.foo(A());
    
    //由于静态成员变量不在对象中存储,类的大小为非静态成员变量内存对齐的结果,A只有一个非静态成员变量
	cout << "sizeof(A) = " << sizeof(A) << endl;

	return 0;
}

五、总结

A无论是类还是全局函数,只要A被定义为B的友元,A就可以访问B的非公有成员。

  • 14
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值