Thursday, November 11, 2010

keyword "static" in C and C++


 What are the meanings of keyword "static" in C and C++?
 1, allocate once at a fixed address. If a variable is declared as static, it will be stored in the data segment, not in stack or heap. This is the concept of static storage.

2,  Local to a particular translation unit. The variable can't be seen outside the function or class. This describes its concept of linkage: internal linkage. (const also has internal linkage in C++, but external linkage in C)

- static variables inside the function can retain values between function calls.
- static objects can be initialized either with the constructor argument list or with the default constructor. If you don't specify constructor argument, the class must have a default constructor.
- destructors for static objects are called when main() exits or exit() is called.
- static member function works for the class as a whole rather than for a particular object of the class. You can call the static function by itself without any specific object, using the scope resolution operator.
- a static member functions can't access ordinary data members, only static data members. It can only call other static member functions.

No comments:

Post a Comment