Home > Backend Development > C++ > body text

What is the difference between C++ static functions and global functions?

WBOY
Release: 2024-04-17 08:54:01
Original
1147 people have browsed it

Static functions are only visible within the class, without a class instance, and are used for tool class methods; while global functions are visible within the entire program, without a class or instance, and are used for independent functions.

C++ 静态函数与全局函数有什么区别?

The difference between C static functions and global functions

Introduction
In C, static Functions and global functions are two types of functions with different functions and scopes. Understanding the differences between them is crucial to writing clear and maintainable code.

Static functions
Static functions are similar to ordinary member functions, but they cannot access non-static member data of the class. They are typically used to implement utility class methods that do not require access to the class's state.

Declaration and Definition

class MyClass {
public:
    static int add(int a, int b) { return a + b; }
};
Copy after login

Scope and Callability
Static functions are only visible within the scope of the class. This means that they can be called directly by the class name without creating an instance of the class.

int result = MyClass::add(10, 20); // 输出 30
Copy after login

Global function
Global function does not belong to any class. They are visible throughout the scope of the program. They are used to define class-independent functionality, such as I/O operations or mathematical operations.

Declaration and Definition

int add(int a, int b) { return a + b; }
Copy after login

Scope and Callability
Global functions can be accessed and called from anywhere in the program.

int result = add(10, 20); // 输出 30
Copy after login

Difference table

FeatureStatic functionGlobal function
VisibilityLimited to classProgram scope
Accessibility No need for class instanceNo need for class or instance
ScopeWithin classEntire program
PurposeTool class methodIndependent function

Actual case

Example 1: Static function
Create a Math class that contains a static calculateArea function that calculates The area of ​​a circle.

class Math {
public:
    static double calculateArea(double radius) { return (3.14 * radius * radius); }
};
Copy after login

Call:

double area = Math::calculateArea(5); // 输出 78.5
Copy after login

Example 2: Global function
Define a displayMessage global function, this function Print the message passed to it.

void displayMessage(const string& message) {
  cout << message << endl;
}
Copy after login

Call:

displayMessage("Hello world!"); // 输出 "Hello world!"
Copy after login

Conclusion
Understanding the difference between static functions and global functions is essential for writing clear and maintainable C Code is crucial. Static functions are used for utility class methods within a class, while global functions are used for independent functionality available within the program scope.

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

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!