c语言中++b与b++_C ++中的朋友功能
c语言中++b与b++
Friend Functions in C++ are a category of functions that can access private and protected members of a class while being a public function/outside the class.
C ++中的“朋友函数”是一类函数,可以作为类的公共功能/外部访问类的私有成员和受保护成员。
You may wonder; is this even possible? Well, modern C++ says yes!
您可能想知道; 这有可能吗? 好吧,现代C ++可以!
Let us look at how we can use these functions in C++.
让我们看看如何在C ++中使用这些函数。
C ++中的朋友功能 (Friend Function in C++)
We call a function a friend function if the function definition is prefixed with the friend
keyword.
如果函数定义以friend
关键字为前缀,则将函数称为朋友函数 。
We cannot use the friend
prefix outside a class, so it can only be used in the member function declaration.
我们不能在类外部使用friend
前缀,因此只能在成员函数声明中使用。
For example, if we want to use the friend
keyword in the declaration, we can use it like this:
例如,如果我们想在声明中使用friend
关键字,可以这样使用:
#include <iostream>using namespace std;class MyClass {private:int a, b;public:MyClass(int a, int b) {// Constructorthis->a = a;this->b = b;}void set_a(int val);void set_b(int val);int get_a() {return a;}int get_b() {return b;}// Declare a friend function of this classfriend void my_fun(MyClass& my_obj, int val, char option);
};void my_fun(MyClass& my_obj, int val, char option) {// Friend function that sets the private variables// based on the optionif (option == 'a') {// Set amy_obj.a = val;}else {// Set bmy_obj.b = val;}
}int main() {MyClass my_obj(1, 2);cout << "my_obj.a = " << my_obj.get_a() << " and my_obj.b = " << my_obj.get_b() << endl;// Friend functions can be accessed from outside the class!my_fun(my_obj, 20, 'a');my_fun(my_obj, 40, 'b');cout << "my_obj.a = " << my_obj.get_a() << " and my_obj.b = " << my_obj.get_b() << endl;return 0;
}
Here, my_fun()
is a friend function of MyClass
, so it can access the private members too!
在这里, my_fun()
是MyClass
的朋友函数,因此它也可以访问私有成员!
Notice that we can access a friend function from outside the class as well.
注意,我们也可以从类外部访问friend函数 。
The above friend function sets the value of the private members a
or b
directly!
上面的friend函数直接设置私有成员a
或b
的值!
Output
输出量
my_obj.a = 1 and my_obj.b = 2
my_obj.a = 20 and my_obj.b = 40
A friend function can either be within the same class or even inside another class. But it must be a member function.
朋友函数可以在同一类中,甚至可以在另一类中。 但是它必须是成员函数。
Let’s look at these two cases one by one.
让我们一一看一下这两种情况。
C ++中同一类中的Friend Function (Friend Function within the same class in C++)
The above example showed us that we can use a friend function inside the same class.
上面的例子向我们展示了我们可以在同一个类中使用一个朋友函数。
class MyClass {private:int a, b;public:MyClass(int a, int b) {// Constructorthis->a = a;this->b = b;}void set_a(int val);void set_b(int val);int get_a() {return a;}int get_b() {return b;}// Declare a friend function of this class// within the same class itself!friend void my_fun(MyClass& my_obj, int val, char option);
};
We’ll move on to the next case; when a friend function is inside another class!
我们将继续进行下一个案例。 当一个朋友函数在另一个类里面时!
This is actually inside another topic, called Friend Classes, but they are directly related to each other.
这实际上在另一个主题(称为Friend Classes)中 ,但是它们彼此直接相关。
C ++中的朋友类–另一个类中的朋友函数 (Friend Classes in C++ – Friend Functions in another class)
Any class B which has friend functions of another class A is called a Friend Class of A.
它具有另一个类A的朋友功能的任何类B被称为A的朋友类。
Similar to friend functions, any member function inside the friend class B can access private and protected members of A.
类似于好友函数,好友类B中的任何成员函数都可以访问A的私有成员和受保护成员。
Let’s understand this more clearly using an example.
让我们通过一个例子更清楚地了解这一点。
I will use the previous example, but I will add another class Student
, and make it as the friend class of MyClass
.
我将使用前面的示例,但是我将添加另一个类Student
,并将其作为MyClass
的朋友类。
#include <iostream>
#include <string>using namespace std;class Student;class MyClass {private:int a, b;public:MyClass(int a, int b) {// Constructorthis->a = a;this->b = b;}void set_a(int val);void set_b(int val);int get_a() {return a;}int get_b() {return b;}// Declare a friend function of this classfriend void my_fun(MyClass& my_obj, int val, char option);// Make Student a friend Class of MyClassfriend Student;
};
Now, let’s write the Student
class:
现在,让我们编写Student
类:
class Student {private:string name; int marks;public:Student(string s_name, int s_marks) {// Constructorname = s_name;marks = s_marks;}int get_marks() {return marks;}string get_name() {return name;}void set_marks(Student& stud, int s_marks) {stud.marks = s_marks;}void set_name(Student& stud, string s_name) {stud.name = s_name;}void change_a(MyClass& my_obj, int val) {// You need to pass the object by reference.// Otherwise, it will not reflect the changes on// the original objectmy_obj.a = val;}void change_b(MyClass& my_obj, int val) {// You need to pass the object by reference.// Otherwise, it will not reflect the changes on// the original objectmy_obj.b = val;}
};
As you can see, I am using change_a()
and change_b()
to change the MyClass
object attributes directly!
正如你所看到的,我使用change_a()
和change_b()
来改变MyClass
对象属性直接!
Let’s now run the complete snippet below.
现在,让我们运行下面的完整代码段。
#include <iostream>
#include <string>using namespace std;class Student;class MyClass {private:int a, b;public:MyClass(int a, int b) {// Constructorthis->a = a;this->b = b;}void set_a(int val);void set_b(int val);int get_a() {return a;}int get_b() {return b;}// Declare a friend function of this classfriend void my_fun(MyClass& my_obj, int val, char option);// Make Student a friend Class of MyClassfriend Student;
};class Student {private:string name; int marks;public:Student(string s_name, int s_marks) {// Constructorname = s_name;marks = s_marks;}int get_marks() {return marks;}string get_name() {return name;}void set_marks(Student& stud, int s_marks) {stud.marks = s_marks;}void set_name(Student& stud, string s_name) {stud.name = s_name;}void change_a(MyClass& my_obj, int val) {// You need to pass the object by reference.// Otherwise, it will not reflect the changes on// the original objectmy_obj.a = val;}void change_b(MyClass& my_obj, int val) {// You need to pass the object by reference.// Otherwise, it will not reflect the changes on// the original objectmy_obj.b = val;}
};void my_fun(MyClass& my_obj, int val, char option) {// Friend function that sets the private variables// based on the optionif (option == 'a') {// Set amy_obj.a = val;}else {// Set bmy_obj.b = val;}
}int main() {MyClass my_obj(1, 2);cout << "my_obj.a = " << my_obj.get_a() << " and my_obj.b = " << my_obj.get_b() << endl;// Friend functions can be accessed from outside the class!my_fun(my_obj, 20, 'a');my_fun(my_obj, 40, 'b');cout << "my_obj.a = " << my_obj.get_a() << " and my_obj.b = " << my_obj.get_b() << endl;// Class Student objectsStudent stud("Amit", 34);cout << "stud.name = " << stud.get_name() << " and stud.marks = " << stud.get_marks() << endl;// Change my_obj.a and my_obj.b using the friend classstud.change_a(my_obj, 100);stud.change_b(my_obj, 200);cout << "After using the Friend Class methods,\n";cout << "my_obj.a = " << my_obj.get_a() << " and my_obj.b = " << my_obj.get_b() << endl;return 0;
}
Output
输出量
my_obj.a = 1 and my_obj.b = 2
my_obj.a = 20 and my_obj.b = 40
stud.name = Amit and stud.marks = 34
After using the Friend Class methods,
my_obj.a = 100 and my_obj.b = 200
Indeed, the changes made by the friend
class methods are indeed reflected in my_obj
.
实际上, friend
类方法所做的更改确实反映在my_obj
。
结论 (Conclusion)
Hopefully, that gives you a good idea of what friend functions in C++ are.
希望您能对C ++中的朋友功能有一个很好的了解。
C++ has given us this flexibility to even modify private and protected variables using friend
methods. Make sure you use them wisely!
C ++给了我们这种灵活性,甚至可以使用friend
方法来修改私有和受保护的变量。 确保您明智地使用它们!
参考资料 (References)
- cplusplus.com article on Friend Functions关于朋友功能的cplusplus.com文章
翻译自: https://www.journaldev.com/36329/friend-function-in-c-plus-plus
c语言中++b与b++
c语言中++b与b++_C ++中的朋友功能相关推荐
- c语言 字符串字符反向储存_C ++中的反向字符串
c语言 字符串字符反向储存 In many situations, we may need to reverse a string in C++ programming. It may include ...
- c语言atoll函数怎么用_C ++中带有示例的atoll()函数
c语言atoll函数怎么用 C ++ Atoll()函数 (C++ atoll() function) atoll() function is a library function of cstdli ...
- c语言 函数的参数传递示例_C ++中带有示例的nearint()函数
c语言 函数的参数传递示例 C ++附近的int()函数 (C++ nearbyint() function) nearbyint() function is a library function o ...
- c++中的结构体_C ++中的结构
c++中的结构体 介绍 (Introduction) In this tutorial, we are going to learn the basics of Structures in C++, ...
- python展开 c函数中的宏预处理_C中的预处理宏
C中的预处理宏 宏定义就属于预处理命令的一种.那么,什么是宏呢? 宏:c语言标准允许在程序中用一个标识符来表示一个字符串.标识符就是宏名. 宏替换:宏替换就是宏定义.在编译预处理中,将程序中所有的宏名 ...
- c ++中字符串长度的_C ++中的字符串长度
c ++中字符串长度的 The string length in C++ can be calculated or found by various methods. Here, in this tu ...
- c语言面向对象编程中的类_C ++中的面向对象编程
c语言面向对象编程中的类 Object oriented programming, OOP for short, aims to implement real world entities like ...
- 寻找某个数c语言,C++_C语言实现两个递减数列中寻找某一个数,本文实例讲述了C语言实现两个 - phpStudy...
C语言实现两个递减数列中寻找某一个数 本文实例讲述了C语言实现两个递减数列中寻找某一个数的方法,分享给大家供大家参考之用.具体方法如下: 通常来说这道题算二分查找法中非常有难度的一题了. 题目如下: ...
- c语言静态函数调用静态变量_C语言中的静态变量和函数
c语言静态函数调用静态变量 C中的静态变量 (Static Variables in C) Basically, when static variables are declared, they cr ...
最新文章
- 打造全球最大规模 Kafka 集群,Uber 的多区域灾备实践
- Docker命令基础 简洁版本
- zookeeper源码分析之三客户端发送请求流程
- Kotlin中?和!!的区别
- 大数据WEB阶段 后台和页面之间传递日期格式数据的400问题
- pytorch打印模型参数_Pytorch网络压缩系列教程一:Prune你的模型
- java测试不成功_java – 测试@NotNull时集成测试失败
- Continuous Laplacian, Functional Map, Spectral CNN
- 人工智能都这么火了,底层基础架构还有必要开源吗?
- 捷径app 保存视频_Android N App捷径
- 如何在 iOS 15 和 macOS Monterey 的 Safari 中隐藏 IP 地址?
- 【项目经验】EasyUI Tree
- 自定义填充图案插件 cad_CAD填充技巧:填充图案
- Win10快捷键模式退出的方法
- 三菱PLC漏型源型总结
- PMP的含金量价值主要表现在哪些方面?
- matlab根据脉冲计算转速,求不规则脉冲之前的时间间隔,进而求出电机转速
- 详解python使用browsermobproxy获取当前网页xhr的get数据方法
- java基础热门侠客养成_侠客养成手册攻略大全 新手攻略开局任务流程汇总[多图]...
- 如何将PDF转换成Word文档?教你3种方法