Home > Backend Development > C++ > body text

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

WBOY
Release: 2024-04-28 21:42:02
Original
1137 people have browsed it

Friend functions can access private and protected data members of the class, but global functions cannot. Friend functions are declared in the class declaration, and global functions are declared outside the class. Use the friend keyword to declare friend functions and the extern keyword. Declare global functions. Friend functions obtain permission to access class member variables through declaration. For example, by declaring a friend function getPrivateData, you can access the private variable x of the MyClass class.

C++ 友元函数详解:友元函数和全局函数的区别?

C Detailed explanation of friend functions

The difference between friend functions and global functions

In C, a friend function is a special function that can access private and protected data members declared in a class member function. In contrast, global functions are declared outside the class and have no access to private and protected data members.

The syntax of friend function

The syntax of friend function is as follows:

friend 返回类型 函数名(参数列表);
Copy after login

For example, declare a function that can access MyClass Friend functions of private data members:

friend int getPrivateData(MyClass& object);
Copy after login

The difference between friend functions and global functions

The main differences between friend functions and global functions are as follows:

  • Access scope: Friend functions can access private and protected data members in the class, but global functions cannot.
  • Scope: Friend functions can be declared in the declaration of the class, while global functions are declared outside the class.
  • Declaration method: Friend functions are declared using the friend keyword, while global functions are declared using the extern keyword.

Practical case

Consider a MyClass class that contains private member variables x:

class MyClass {
private:
    int x;
public:
    int getX();
    void setX(int value);
};
Copy after login

To access the private data members x of MyClass, we can declare a friend function:

friend int getPrivateData(MyClass& object) {
    return object.x;
}
Copy after login

Use friend function

We can use friend functions to access the private data members of MyClass:

int main() {
    MyClass object;
    object.setX(10);
    int privateData = getPrivateData(object);
    cout << "Private data: " << privateData << endl;
    return 0;
}
Copy after login

Running this code will output:

Private data: 10
Copy after login

The above is the detailed content of Detailed explanation of C++ friend functions: What is the difference between friend functions and global 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!