C + + static local variables and static functions examples _c language

Source: Internet
Author: User

A variable is defined in the body of the function that allocates stack memory to the local variable whenever the program runs to that statement. However, as the program exits the function body, the system will reclaim the stack memory and the local variables should be invalidated accordingly. But there are times when we need to save the value of a variable between two calls. The usual idea is to define a global variable to implement. But in this way, the variables are no longer part of the function itself, and are no longer only controlled by functions, causing inconvenience to the maintenance of the program. Static local variables can just solve the problem. Static local variables are saved in the global data area, not on the stack, and the value is persisted to the next call until the next time the new value is assigned. The variable allocates memory in the global data area; a static local variable is initialized for the first time when the program executes to the declaration of the object, which is very important that a subsequent function call is no longer initialized (which is very significant); The static local variable is typically initialized at the declaration and, if not explicitly initialized, is automatically initialized to 0 by the program ; it always resides in the global data area until the program has finished running. But its scope is a local scope, when the function or statement block that defines it ends, its scope ends; the static function is used primarily to access static members, not to directly access the non-static members of the class; static member functions are called conveniently and are not required to generate objects. Like what

Copy Code code as follows:

Class X
{
Public
void MethodA ();
static void MethodB ();
}

At this time MethodB can be called directly, X::methodb ();
And MethodA must be able to be invoked after the class object, x x; X.methoda ();

Copy Code code as follows:

Example 3

#include <iostream.h>
VOID Fn ();

void Main () {
FN (); FN (); FN ();
}

VOID Fn () {

static int n=10; is initialized only the first time it is invoked, and the second call is ignored
cout<<n<<endl;
n++;
}



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.