Home  >  Article  >  Backend Development  >  Detailed explanation of C++ friend functions: What is the difference between friend functions and member functions?

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

王林
王林Original
2024-04-29 08:27:011077browse

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();
  ...
};

Member functions

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

Grammar:

class MyClass {
  void myMemberFunction();
  ...
};

The difference between friend function and member function

Features Friend function Member function
Accessibility Can access private and protected members of the class Can access all members of the class
Declaration location Outside the class Inside the class
Scope Global Internal class
Calling method Call like a normal function Call using class objects

Practical case

Consider a Student class that has a private marks Member:

class Student {
private:
  int marks;
  ...
};

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;
}

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;
}

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!

Statement:
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