The difference between a static member and an instance member of a class in C + +

Source: Internet
Author: User

In C + + classes, static data members and (static) function members defined by static are classes, whereas members and functions that are not defined by static are called instance data members and instance function members, and they belong to an instance (object).

In memory, a static data member of a class occupies a specific memory space and is shared by all instances (objects) of that class. Instance data members of different objects of the same class are independent of each other and occupy their own storage space.

After you define a class, you must define static data members in the scope of the file outside the class, that is, allocate storage space for them.

You can use the class name and domain resolution operators directly in your program: direct access to common static data members and common function members of a class, and cannot directly access private members of a class.

You cannot initialize a static data member in a class's constructor, because a static data member does not belong to a specific class.

In a static function member of a class, you can access only the static data members of the class, not the instance data members of the class, because a concrete instance (object) of the class may not have been created when a static function member is called.

The difference between a static member and an instance member of a class in C + +

Related Article

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.