4

Can anybody explain difference between static function defined within class and static function declared e.g. in file.hpp and defined in file.cpp (I can only use this static function within this file ?

bladzio
  • 414
  • 3
  • 15
  • possible duplicate of [Difference between Static variable declared in different scopes](http://stackoverflow.com/questions/18140552/difference-between-static-variable-declared-in-different-scopes) – Jerry Coffin Nov 18 '13 at 14:27
  • 1
    That's about variables; this is about functions. – Mike DeSimone Nov 18 '13 at 14:49

3 Answers3

8

Can anybody explain difference between static function defined within class

That means the function is class-wide, and doesn't need to operate on a particular object. In other words, for that function there is no this.

and static function declared e.g. in file.hpp and defined in file.cpp (I can only use this static function within this file ?

That means that that function does not have external linkage, which means other compilation units (i.e. object files) cannot link to it, because it's not in the symbol table.


Thanks for your reply but could you explain why other compilation units cannot link to it ?

First, some terms. Technically, the compiler is just the part that generates object code from source code. The linker later takes a set of object files and "links" them to make the final program.

To make this work, the compiler generates a "symbol table" and puts it in the object file along with the compiled code. This symbol table lists both the symbols for the global variables and functions in the file, as well as the external symbols that code needs to be linked to in order to work.

The linker's job is to read all the object files and match symbols needed by each object file to symbols provided by other object files. If everything is successful, and there aren't any unresolved needed symbols, the link succeeds and you get your program.

What static on a function or global does is simply tell the compiler to not put that symbol in the object file's symbol table. Nothing else; that symbol is still perfectly usable within that same source file. The linker simply never sees the symbol, and thus cannot link anything to it.

Class members cannot be "disappeared" in this manner, so static has a different meaning in the context of a class. (This recycling of the keyword was probably done to avoid adding another reserved word to the language. BTW, Objective-C solved this same problem in a different manner, using the + and - tokens.)

(And static can have yet another meaning when applied to variables declared inside functions or methods, as Mike points out below. In that case it's basically a global variable, but private to the function.)


Could you also explain why inline functions are implicitly defined as static ?

Since inline functions do not exist as independent pieces of code (they are instead merged "in line" into the calling function), they cannot have symbol table entries (there's nothing to link to).

Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
  • Thanks for your reply but could you explain why other compilation units cannot link to it ? Every e.g. function in some compilation unit product some symbol, unless we have static word before function name - does it change something ? – bladzio Nov 18 '13 at 16:40
-1

There is no difference between a static function defined in the global scope, no matter if it's in a header file or in a source file. Unless the header file isn't included anywhere, when the functions in it is never really defined anywhere.

Then phrase you need to learn when talking about static (non-member) functions is translation unit. A translation unit is a source file and all header files included in that source file, after the preprocessor have processed the file and is the actual input to the compiler. A static function is local to the translation unit, which is why there is no difference if it's defined in the source file or a header file.

You can also use an anonymous namespace to define functions, and they will be local to just the translation unit the anonymous namespace is in.

Also note that functions defined as inline are implicitly defined as static as well.


A static member function is part of the class, and can access static member variables without scope prefix. They do of course have to prefixed with the scope of the class to be called. The difference between a static member function and a non-static member function is that static member functions are not part of any specific instance of the class, and so have no this pointer. If you want to access a specific class instances member variable, you have to pass the instance to the static member function through an argument.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks for your reply. I was concerned more with the fact that when I want to use static function (e.g. which I have declaration in file.hpp and definition in file.cpp ) in another compilation unit and it doesn't work. When I create in this file class and I move there this static function and I use it in another compilation unit it is ok, why ? When I don't have static function in class or structure it is similar to that when I have in compilation unit anonymous namespace ? If I have only static function in some file I don't have it in the symbol table ? – bladzio Nov 18 '13 at 16:53
  • Could you also explain why inline functions are implicitly defined as static ? – bladzio Nov 18 '13 at 16:54
  • **−1** “Also note that functions defined as inline are implicitly defined as static as well.” No, an `inline` function at namespace scope has external linkage by default. Also, the first sentence does not make any sense to me, “There is no difference between a static function defined in the global scope, no matter if it's in a header file or in a source file”. – Cheers and hth. - Alf Feb 14 '15 at 08:09
-1

Please refer to this link

A static variable inside a function keeps its value between invocations. In C++, however, static is also used to define class attributes (shared between all objects of the same class) and methods. In C there are no classes, so this feature is irrelevant.

Community
  • 1
  • 1
Mike
  • 59
  • 3