CC ++ language Static

xiaoxiao2021-03-06  41

In the C language, Static has two meanings: (1) If static int foo; this sentence is in a function, Static represents the storage attribute, indicating that foo is a static variable. Placed in a static storage area only accounts for one part. Its survival cycle is as long as the program. (2) If static int foo; this sentence is outside the function, the foo is a global variable, static is no longer a storage property, but is used as a restriction: to limit the visible range of the global variable FOO, The domain is limited to the file in the file, which is invisible in other files. Static void func (); indicating that the function is only visible in this document.

In C , Static members in the class represent all objects shared storage area. Static member functions can only access static members. Static member initialization must be placed in the file range, even private members. Static member function calls can be used with object member functions, or use class member functions.

// EXAMPLE of The Static Keyword

Static Int i; // variable accessible Only from this file

Static void func (); // function accessible Only from this file

INT MAX_SO_FAR (Int Curr)

{

Static int biggest; // variable whose value is retained

// Between Each Function Call

IF (Curr> Biggest)

Biggest = CURR;

Return Biggest;

}

// c only

Class savingsaccount

{

PUBLIC:

Static void setInterest (Float newValue) // Member Function

{Currentrate = newValue;} // That Accesses

// only static

//Members

Private:

CHAR Name [30];

Float Total;

Static float currentrate; // one copy of this Member IS

// Shared Among All Instances

// of savingsaccount

}

// Static Data MEMBERS MUST BE Initialized At File Scope, Even

// if private.

Float savingsaccount :: currentrate = 0.00154;

转载请注明原文地址:https://www.9cbs.com/read-72792.html

New Post(0)