search
HomeBackend DevelopmentC++What is the difference between C++ static functions and friend functions?

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.

C++ 静态函数与友元函数有什么区别?

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

##Class membershipisNotAccess rightsClass static membersAll members of the classthis pointerNot acceptedCan be receivedDeclaration method
Features Static function Friend functions
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!

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
C++ 静态函数与全局函数有什么区别?C++ 静态函数与全局函数有什么区别?Apr 17, 2024 am 08:54 AM

静态函数仅在类内可见,无需类实例,用于工具类方法;而全局函数在整个程序范围内可见,无需类或实例,用于独立功能。

C++ 静态函数与动态函数的比较和取舍C++ 静态函数与动态函数的比较和取舍Apr 17, 2024 am 10:48 AM

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

C++ 友元函数详解:友元函数在多继承中的作用?C++ 友元函数详解:友元函数在多继承中的作用?Apr 29, 2024 pm 06:39 PM

友元函数允许非成员函数访问私有成员,并在多继承中发挥作用,允许派生类函数访问基类的私有成员。

C++ 静态函数可以被继承吗?C++ 静态函数可以被继承吗?Apr 17, 2024 am 08:36 AM

不可以。静态函数与类无关,因此不会被继承下来,继承只适用于实例成员,而不适用于静态成员。

C++ 静态函数的性能考虑有哪些?C++ 静态函数的性能考虑有哪些?Apr 16, 2024 am 10:51 AM

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

C++ 友元函数详解:友元函数用于解决哪些问题?C++ 友元函数详解:友元函数用于解决哪些问题?Apr 28, 2024 pm 05:06 PM

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

友元函数对类的封装性有什么影响?友元函数对类的封装性有什么影响?Apr 17, 2024 am 10:12 AM

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

C++ 友元函数如何访问私有成员?C++ 友元函数如何访问私有成员?Apr 15, 2024 pm 05:27 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

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.