In this tutorial, we will learn what is a static function in C++, its characteristics and when we should use a static function in our program?

What is static function in C++?

A function which is declared static using ‘static‘ keyword becomes a static function in C++.

Syntax:

A static function when declared inside a class:

  • Can be accessed outside the class using class name and scope resolution operator (::), without creating any object.
  • Can only access static members of the class.
  • Can’t call any other class functions that are not static.
  • All objects of the class share the same copy of the static function.

Let see the example for clear understanding.

C++ Static Function Example

Recommended: Static Variable in C++

Output

If you have any suggestion or doubt then comment below.

Leave a Reply

Close Menu