本文共 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 ++中使用这些函数。
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
关键字,可以这样使用:
#includeusing 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函数直接设置私有成员a
或b
的值!
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.
让我们一一看一下这两种情况。
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)中 ,但是它们彼此直接相关。
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
。
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
方法来修改私有和受保护的变量。 确保您明智地使用它们!
翻译自:
c语言中++b与b++
转载地址:http://xgozd.baihongyu.com/