博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言中++b与b++_C ++中的朋友功能
阅读量:2529 次
发布时间:2019-05-11

本文共 9218 字,大约阅读时间需要 30 分钟。

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 
using namespace std;class MyClass { private: int a, b; public: MyClass(int a, int b) { // Constructor this->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 friend 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 option if (option == 'a') { // Set a my_obj.a = val; } else { // Set b my_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函数直接设置私有成员ab的值!

Output

输出量

my_obj.a = 1 and my_obj.b = 2my_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) {            // Constructor            this->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 
#include
using namespace std;class Student;class MyClass { private: int a, b; public: MyClass(int a, int b) { // Constructor this->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 friend void my_fun(MyClass& my_obj, int val, char option); // Make Student a friend Class of MyClass friend Student;};

Now, let’s write the Student class:

现在,让我们编写Student类:

class Student {    private:        string name;           int marks;    public:        Student(string s_name, int s_marks) {            // Constructor            name = 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 object            my_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 object            my_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 
#include
using namespace std;class Student;class MyClass { private: int a, b; public: MyClass(int a, int b) { // Constructor this->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 friend void my_fun(MyClass& my_obj, int val, char option); // Make Student a friend Class of MyClass friend Student;};class Student { private: string name; int marks; public: Student(string s_name, int s_marks) { // Constructor name = 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 object my_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 object my_obj.b = val; }};void my_fun(MyClass& my_obj, int val, char option) { // Friend function that sets the private variables // based on the option if (option == 'a') { // Set a my_obj.a = val; } else { // Set b my_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 objects Student 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 class stud.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 = 2my_obj.a = 20 and my_obj.b = 40stud.name = Amit and stud.marks = 34After 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)

  • on Friend Functions

    关于朋友功能的


翻译自:

c语言中++b与b++

转载地址:http://xgozd.baihongyu.com/

你可能感兴趣的文章
mysql 日期时间运算函数(转)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>
POJ 3740 Easy Finding (DLX模板)
查看>>
MySQL 处理重复数据
查看>>
关于typedef的用法总结(转)
查看>>
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>