Static__c++ of C + + keywords

Source: Internet
Author: User
Tags class definition modifier
first, one question is: why do I need to be static.

1, sometimes programmers have a need: the variables I want to write only in my current file can be accessed. I don't want other files to access my variables.
You can then define the static variable. At this point the static function is (
limits the scope of global variables ).

2, sometimes there are other requirements: I want to have a function within the definition of their own local variables, but I think the function after the execution of the variable does not disappear.
Static to solve this problem. At this point the static effect is (
extend the life cycle of local variables ). Two, static: statically, has two levels of meaning

The position of the variable in memory must be the data segment. There is no static variable on the stack segment.

1. Relative to global variables, "static" limits the range of variables you define.
2, relative to local variables, "static" means that its memory is not used to open, but has been allocated at the beginning. three, static modifier variable

static modifier variable. Therefore, all conditions for the variable are met. For example: I define a static int A; then you can't define int A; Because the name conflicts. Iv. Summary

Static solves the embarrassment of two C + +, global & Local.

1, the scope of global is too large. So programmers want to limit the scope of global, you can use static.
2, the local life cycle is too short. So programmers want to expand the local life cycle, with static. What role does static assume in object-oriented programming design?

1, in the design of the class. We sometimes want a variable to belong to a class. Rather than belonging to the object. It is owned by this class clan. This variable is accessible to objects defined by the class.
Then use static.
2. Static member function: Only used to access static variables. It has no this pointer. Because, this can cause ambiguity.

"1" Use instances

  1/************************************************************************* 2┊file Name:main.cpp 3┊autho R:bin.wang 4┊mail:sa615168@mail.ustc.edu.cn 5┊created time:thu Mar 2017 11:21:00 AM CST 6 ******* /7 8 #include <iostream> 9 using namespace
 Std
 One class a{public:13 int func () {14┊a = 7;
 15┊b = 7;
 16┊cout << a<< b << Endl;
 public:19 static int b;
 int A;
 21};   a::b int = 2;
 class declaration, class outside definition 24//Actually, here we can think about the reason why we have to declare outside the class.
 int main ()-HH;
 Hh.func ();
 return 0; 30 31}/* Answer: This is also my experience in practice, I hope to share with you. The end of the paper. Suppose we can have a constructor within the class (to assign a value to this B), and C + + first want to use the static variable, it is hoped that as long as the class definition of variables, all of his defined objects share the variable, that is, these objects can be "see" or "modify" this way, each time By defining an object with this class, the static object "goes back to the original starting point." It's not messed up.

  The other object is just using the value of the static variable, good boy, let the new class get rid of the value, become the initialization of the case. Moreover, we can also sendNow, the static variable can only be initialized once. 

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.