What does the C + + learning---static do?

Source: Internet
Author: User

How does static work?

In the interview, it is easy to ask the basic questions, but also the most easily ignored by everyone.

member variables of the static modifier class

A variable appears in class, such as:

Class Example

{

Public

  static int A;

...

};

When a member variable is static, the static member variable belongs to the entire class, not to an object. The location of the storage, but also with ordinary variables have a difference:

Ordinary member variables, stored in the stack, each instantiated object's member variables, save their own data separately.

The static member variable stores only one copy of the RW segment that is placed in the data segment

Declaration of the static member variable:

Static data type variable name; Inside the class

Initialization of static member variables:

Data type class Name:: static data member = initial value; Static member variables must be initialized! Initialize outside of class

Second, static modifier member function

Static member functions, such as:

Class Example

{

Public

static int A;

Public

  static int changeavalue (int value);

};

The format of the member function of the static decorated class:

Static member function name ();

The meaning of the static member function is not in information sharing, but in managing the static member variable and completing the encapsulation of the static member variable;

The static member function can access only data members at rest. Cause: A non-static member function that is called when the This pointer (the this pointer points to the current instance itself) is passed in as a parameter, whereas a static member function belongs to a class, not an object.

There is no this pointer.

What does the C + + learning---static do?

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.