Static functions are class methods that only access static members and do not receive this pointers; friend functions do not belong to the class and can access all members and receive this pointers.
The difference between static functions and friend functions in C
Static functions
- Belongs to a class but does not belong to any specific object. It can also be called a class method.
- Declared using the static keyword.
- Only static members of the class can be accessed.
- will not receive the this pointer.
Friend function
- Does not belong to any class.
- Use the friend keyword statement.
- Can access all members of the class, including private members.
- can receive this pointer.
Table summary
Features | Static function | Friend functions |
---|---|---|
is | Not | |
Class static members | All members of the class | |
Not accepted | Can be received | |
static Keyword | friend Keywords |
Practical case
Static function example: Calculate circle Area
class Circle { public: static double calculateArea(double radius) { return 3.14 * radius * radius; } }; int main() { double radius = 5.0; double area = Circle::calculateArea(radius); cout << "圆的面积:" << area << endl; return 0; }
Friend function example:Print the value of private member
class Student { private: int age; public: friend void printAge(Student& student); }; void printAge(Student& student) { cout << "年龄:" << student.age << endl; } int main() { Student student; student.age = 20; printAge(student); return 0; }
The above is the detailed content of What is the difference between C++ static functions and friend functions?. For more information, please follow other related articles on the PHP Chinese website!

静态函数在编译时绑定,无需对象实例,可访问静态成员和全局变量,不可继承;动态函数在运行时绑定,需要对象实例,可访问非静态成员和局部变量,可继承。

静态函数性能考虑如下:代码大小:静态函数通常更小,因为不包含成员变量。内存占用:不属于任何特定对象,不占用对象内存。调用开销:更低,无需通过对象指针或引用调用。多线程安全:通常线程安全,因为不依赖于类实例。

友元函数是C++中可访问其他类私有成员的特殊函数。它们解决类封装带来的访问限制,用于解决类间数据操作、全局函数访问私有成员、跨类或编译单元代码共享等问题。用法:使用friend关键字声明友元函数,可访问私有成员。注意:谨慎使用友元函数,避免绕过封装机制带来的错误。仅在必要时使用,限制访问权限,谨慎使用修改器函数。

友元函数对类的封装性有影响,包括降低封装性、增加攻击面和提高灵活性。它可以访问类的私有数据,如示例中定义为Person类的友元的printPerson函数可以访问Person类的私有数据成员name和age。程序员需权衡风险与收益,仅在必要时使用友元函数。

C++中友元函数访问私有成员的方法有两种:在类内声明友元函数。声明一个类作为友元类,该类中所有的成员函数都可以访问另一个类的私有成员。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
