C + + static objects

Source: Internet
Author: User

A: What is a static object? Objects are stored in a static manner.
    static objects for local static objects and classes.     local static objects: A variable is defined inside a function whose lifecycle spans multiple invocations of the function. Local objects are guaranteed not later than in the program execution flowinitialized once a defined statement of the object is passed. Once such objects are created, they are not destroyed until the end of the program. When a function that defines a local static object finishes executing, the static object is not undone. When the function is called more than once, the static local object persists and saves its value.
Static member objects: Static member objects are stored in the global store, and the scope of the static member objects is in the scope of the class. It can avoid conflicts with other class members or global object names. Can be encapsulated as private, while global objects are not allowed.
II: Definition and initializationallows the initial value to be assigned to the static local amount of the constructed class. If the initial value is not assigned, it is automatically assigned by the system. The numeric variable automatically assigns the initial value 0, and the character variable assigns a null character. The system automatically assigns a 0 value to a static local variable of the underlying type if it is not assigned the initial values in the description. And the automatic variableIf the initial value is not assigned, the values are variable.
    • Local Static objects:
  
 
  1. int fun(int i)
  2. {
  3. static int a = 10;
  4. a = a+i;
  5. return a;
  6. }
  7. int main(int argc, char *argv[])
  8. {
  9. for(int i=0;i<3;i++)
  10. {
  11. cout <<fun(i)<<endl;
  12. }
  13. }

    • Static Class member objects
declaration: Static member variable declarations of a class are statically and are also subject to public/private access rules. because static members are not part of any object, they cannot be initialized by construction. should be defined and initialized separately.
  
 
  1. class Account {
  2. public:
  3. // interface functions here
  4. void applyint() { amount += amount * interestRate; }
  5. static double rate() { return interestRate; }
  6. static void rate(double); // sets a new rate
  7. private:
  8. std::string owner;
  9. double amount;
  10. static double interestRate;
  11. static double initRate();
  12. }; each object of this class has two data members: Owner and amount. The object does not have a data member corresponding to the static data member, but there is a separate InterestRate object that is shared by the entire object of the account type.
The static data member must be defined outside the body of the class definition (exactly once). Unlike regular data members,static members are not initialized by the class constructor, but should be initialized at the time of definition.
Definition: defined in the file of a function definition, the defined format:    type name Class Name:: Object name = value;(no more static in front, The static key word can only be used in declarations inside the class definition body )
 
   
  
  1. double Account::interestRate = initRate();
This statement defines a static object named InterestRate, which is thefor double type. The indication behind the scope character is under the scope of the class, so you can use the Initrate () function directly. Note that although initrate is private, we can also initialize interestrate with it. As with other members, the definition of interestrate can also be accessed by private members of the class. Access/Invoke: can be used directly in the member function of the class to which the variable belongs, through the class name in other files:: Function name/variable
The difference between global static objects and global variables a global variable is preceded by a global variable with static. Global variables themselves are static storage, and static global variables are also stored in static mode. The difference between the two is that the scope of the non-static global variable is the entire source program, and when a source program consists of multiple source files, non-static global variables are accessible in each source file. Static global variables are only available in files that define the static global variable and are inaccessible in other source files of the same source program. Therefore, you can avoid causing errors in other source files.
after the local variable is changed to a static variable, it changes its life cycle. The global variable is changed into a static global variable, and its scope is limited.

Iv. the most classic use of static member variables is in design modesingle-case modeup. In the next article, I'll point out three ways to write a singleton pattern, and its pros and cons.

    


From for notes (Wiz)

C + + static objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.