Home > Backend Development > C++ > body text

Detailed explanation of C++ friend functions: What is the difference between friend functions and member functions?

王林
Release: 2024-04-29 08:27:01
Original
1069 people have browsed it

Friend functions allow external functions to access private or protected members of a class by declaring them with the friend keyword in the class definition. Unlike member functions, friend functions are declared outside the class and can access private and protected members of the class, while member functions are declared inside the class and can access all members of the class. Friend functions are used as ordinary function calls, while member functions are called with class objects. Friend functions are used when external access to private or protected members is required, and member functions are used when member functions are used inside the class.

C++ 友元函数详解:友元函数和成员函数的区别?

C Detailed explanation of friend functions: The difference between friend functions and member functions

Friend functions

A friend function is a special function that allows external functions to access private and protected members in a class. It is declared using the friend keyword in the class definition.

Syntax:

class MyClass {
  friend void myFriendFunction();
  ...
};
Copy after login

Member functions

Member functions are functions that belong to a class and can access its private and protected members.

Grammar:

class MyClass {
  void myMemberFunction();
  ...
};
Copy after login

The difference between friend function and member function

FeaturesFriend functionMember function
AccessibilityCan access private and protected members of the class Can access all members of the class
Declaration locationOutside the classInside the class
ScopeGlobalInternal class
Calling methodCall like a normal functionCall using class objects

Practical case

Consider a Student class that has a private marks Member:

class Student {
private:
  int marks;
  ...
};
Copy after login

We can use the friend function calculateAverage() to calculate the student's average grade, which has access to marks:

// 友元函数
friend double calculateAverage(Student& student);

// 计算学生的平均成绩
double calculateAverage(Student& student) {
  return student.marks / 3;
}
Copy after login

We can also use member functions to calculate the average grade, but it can only be used in the Student class:

// 类的成员函数
double getAverage() {
  return marks / 3;
}
Copy after login

Conclusion

Friend functions provide a mechanism that allows external functions to access private and protected members of a class without making these members visible to the outside world. Member functions have access to all members of the class, but can only be used within the class.

The above is the detailed content of Detailed explanation of C++ friend functions: What is the difference between friend functions and member functions?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!