Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions about the static keyword with functions and data

Tags:

c++

static

I have a few questions about the static keyword in C++ (and probably with other languages as well.) What is the purpose of declaring a function as static?

void static foo(int aNumber) {
... }

How about a static inline function?

void static inline foo(int aNumber) {
... }

Is there any benefit to using the static keyword with a function, and do those benefits apply to class functions as well? I realize some datatypes like structs and arrays have to be static when compiling with an older compiler, but is there any point when using a new ANSI-C++ compiler (like MS VC++ 2008)? I know that using a static variable inside a loop saves time by keeping the data in memory and not reallocating memory every loop iteration, but how about when a variable is declared only once, like at the top of a header file or within a namespace?

like image 274
Dooms101 Avatar asked Feb 10 '10 14:02

Dooms101


People also ask

How does the keyword static affect variables and functions?

Static Variable in Java Once the variable is declared as static, memory is allocated only once and not every time when a class is instantiated. Hence you can access the static variable without a reference to an object.

What does the static keyword do to a field or function?

In the Java programming language, the keyword static means that the particular member belongs to a type itself, rather than to an instance of that type. This means we'll create only one instance of that static member that's shared across all instances of the class.

What is the need of static keyword for a main () function?

In Java, static keyword is mainly used for memory management. It can be used with variables, methods, blocks and nested classes. It is a keyword which is used to share the same variable or method of a given class. Basically, static is used for a constant variable or a method that is same for every instance of a class.

Which keyword is used with static function?

In C, functions are global by default. The “static” keyword before a function name makes it static. For example, below function fun() is static.


1 Answers

Depends on Context:

Like many things in C++, static means different things depending on its context.

It's very common in C++ for the same word to mean different things depending on its context.
For example:

  • * is used for multiplication, dereferencing a pointer, and creating pointers.
  • & is used to get the address of variables, to declare a reference, and as a bitwise AND operator.

Global use of static:

If you declare a function or variable as static outside of a class and in global scope, it is specific to only that file. If you try to use that variable or function in a different file (via a forward declaration) you will get a linking error.

Example:

a.cpp:

static void fn()
{
  cout<<"hello a!"<<endl;
}

b.cpp:

void fn();
void gn()
{
  fn();//causes linking error
}

This feature allows you to use a function that no other file will ever see, that way you don't cause possible linker errors of a symbol defined multiple times. The preferred method to do this is with anonymous namespaces though:

a.cpp:

namespace
{
  void fn() // will be static to a.cpp
  {
    cout<<"hello a!"<<endl;
  }
}

Inside of a class use of static:

If you declare a function or variable as static inside of a class (or struct), it is a class function or class variable. This means that there is only one for that whole class. A class function can only use class variables. A class variable is shared amongst all instances of that class.

class C
{
public:
  static void fn()
  {
    y = 4;//<--- compiling error
    // can't access member variable within a static function.
  }

  int y;
}

This is a great feature to use if you have something that is specific to the class of your objects, but not specific to an instance.

Inside a function use of static:

If you declare a variable as static inside of a function, you can consider that the variable value will persist upon calls. It will only be initialized once.

Example:

//Will print 0, then 1, then 2, ...
void persistentPrintX()
{
  static int x = 0;
  cout << x << endl;
  x++;
}

I personally try to avoid this, and you probably should to. It is not good to have global state. It is better to have functions that given the same input guarantees the same output.

Just like in the English language:

The concept of context sensitive meaning is not specific to C++, you can even see it in the English language.

  • I am going to screen a movie (Means showing the movie)
  • The screen on the TV is broken (Means a part of the TV)

Other meanings in other programming languages:

Depending on the programming language there can be a different meaning, but the first thing most people think of when you say static is a class variable/function vs a member variable/function.

like image 76
Brian R. Bondy Avatar answered Oct 14 '22 07:10

Brian R. Bondy