Get into the habit of writing together! This is the 7th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .
1️⃣Foreword
Knowledge points in this chapter:
- What is Friendship?
- What are the characteristics of Friends?
- Why the concept of friend Yuan?
- What are the categories of friends?
- How to create and use friends?
2️⃣ Answer
✨What is Friends?
- A friend is an ordinary function or class defined outside the class
- It needs to be declared inside the class body
- In order to distinguish it from the member functions of this class, we will prefix the description with a keyword
friend
- Simply put, a friend is a relationship
- A class-function or class-class relationship
✨What are the characteristics of Friendship?
- Friends can access private members of the class. (Similar: Friends are trustworthy and can disclose some of their privacy to friends.)
- Friendships cannot be inherited (similar: your dad's friends are not necessarily your friends)
- Friendships are unidirectional (similar: class A is a friend of class B, but B is not necessarily a friend of A)
- Friendship relationships cannot be passed (similarly: that is, B is a friend of A, C is a friend of B, but C is not necessarily a friend of A.)
✨Why the concept of Friendship?
- We know that classes have encapsulation, that is, data hiding.
- Only the member functions of the class can access the private members of the class.
- Other functions in the source file cannot access private members.
- Then generally we will set up some public interfaces to open to the outside world.
- That is, non-member functions can access public members of the class.
- But sometimes, class access can be a bit rigid in some applications.
- If the data members are defined as public, which destroys the hidden feature.
- So there is the concept of friends .
- Friends are granted access to private parts of the class from the outside (i.e. "backdoors" some functions outside the class)
- This also increases the flexibility of the class's public interface.
- But it should be noted that friends destroy the overall operability of the class, and also destroy the encapsulation of the class.
- So we have to choose to use friends
✨What are the categories of Friends?
Classification:
- friend class
- friend function
友元关系可以是一个函数,该函数被称为友元函数。
友元也可以是一个类,该类被称为友元类。
✨如何创建和使用友元?
关键字:frend
我们需要在类体内进行说明,为了与该类的成员函数加以区别,在说明时前面加以关键字friend
下面着重看看友元类以及友元函数!
3️⃣友元类
首先我们定义一个类:
这个myBuilding
类就相当于我的家,现在定义另一个类,是我的朋友类
并且我的朋友想访问我家的客厅和卧室
很显然,不可访问!
这是因为,在
myBuilding
这个类中,我的卧室是一个私有的,外部无法访问。但是如果我们在
myBuilding
类中声明myFriend
这个类是友元关系的,即friend class myFriend;
那么此时
myFriend
这个类就是一个友元类,它可以访问类似私有成员。
所以完整代码如下:
运行结果为:
朋友在访问:我的客厅
朋友在访问:我的卧室
复制代码
4️⃣友元函数
一旦我们将一个类声明为另一个类的友元,就意味着这个类的所有成员函数都是另一个类的友元函数,即都可以访问另一个类中私有成员或保护成员。
但有时候我们并不想让类的所有成员都能访问另一个类的私有成员,那么此时我们可以将类的部分成员函数声明为友元函数,这样就只有友元函数可以访问另一个类的私有成员, 而不是整个类都能访问另一个类的私有成员。
因此总结的说,友元函数就是能够访问另一个类的私有成员的一个函数。友元函数从语法上看,它与普通函数一样,即在定义上和调用上与普通函数一样。
For example, in myBuilding
this class, we declare that it visit1
is a friend function, so we visit1
can access myBuilding
the private membersm_BedRoom
And in the myFriend
class, it visit2
is impossible myBuilding
to access the private members in
So in the main function something like this:
5️⃣Write at the end
Well, after reading this article, I believe you have understood the relationship between friends, welcome to the comment area to discuss together!