Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static member vs static global

Tags:

c++

linker

I have read that the difference between globals and static globals is that the global variable can be referred to in another implementation file via extern, whereas static globals are localized to only that implementation file. See these two questions for more information: [1, 2].

From what I understand, this means the that the following foo() and bar() should be linked identically. Both functions can only be used by MyClass.

//MyClass.h
Class MyClass{
private:
  static void foo();
};

//MyClass.cpp
void MyClass::foo(){}
static void bar(){}

I can see foo()'s declaration being more common since it lets the header file lay out the entire class more completely (even if you can't/shouldn't use the private stuff), but is bad practice declare a function like bar() (hidden from the header file)?

For context, I am defining a WNDPROC for windows messages which needs to be static to work, but it's a rather ugly declaration and I'm not sure if I should hide it completely in the implementation file or go ahead and declare it in the header file.

like image 909
Suedocode Avatar asked Jul 29 '13 15:07

Suedocode


People also ask

What is the difference between static and global static?

Difference between static global and static local variables A static global variable can be used anywhere inside the program while a static local variable can only be used inside the scope in which it is declared.

Are static members global?

A static variable can be either a global or local variable. Both are created by preceding the variable declaration with the keyword static. A local static variable is a variable that can maintain its value from one function call to another and it will exist until the program ends.

What is the difference between static and global functions?

Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.

What is difference between local static and global static variables?

A local static variable is created once and the method will go out of scope, but the static variable is not destroyed and it will live in memory until the program ends. A global static variable live in memory until the program ends.


1 Answers

static is a very horrible keyword as it has many different meanings depending on the context. static variables and static functions are completely different, and a static function in a class and a static free-function are completely different.

A static function in a class means that the function can be called without an instance of the class, but it cannot access non-static members of the class. It is a bit like a regular function, just enclosed in the class for tidiness purposes.

A static free-function has internal linkage, so it cannot be seen outside of the source file and its name can be reused in other source files.

A static class function does not have internal linkage. All class functions have external linkage. You can split the class function between header and source files whether the class function is static or not.

I recommend you read some tutorials/books to understand the many different uses of static more clearly. When you see static in a place you've not seen it before, assume nothing!

If you have a free-function which you want hide in a source file, you can declare it static as you have done so. Alternatively you can place it in an unnamed namespace.

// cpp file only
namespace
{
    void hiddenfunc() {..}
}

This is similar to

static void hiddenfunc();

And it can be called in the same way (just as "hiddenfunc()"). An advantage of unnamed namespaces (a weird name, I know) is that you can also place classes and other definitions, that you only want to be visible within that source file. Just make sure you define the function body within the namespace {..} area. Don't put an unnamed namespace in a header file.

like image 156
Neil Kirk Avatar answered Oct 15 '22 23:10

Neil Kirk