The differences between static and instance members of classes in C ++

Source: Internet
Author: User

The differences between static and instance members of classes in C ++

In the C ++ class, static (static) data members and (static) function members belong to the class; on the contrary, members and functions that are not defined by static are called instance data members and instance function members. They belong to a certain instance (object ).

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

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

In a program, you can directly use the class name and domain resolution OPERATOR: directly contains a total of static data members and a total of function members of the class, rather than directly accessing private members of the class.

Static data members cannot be initialized in class constructors, because static data members do not belong to a specific class.

In a static function member of a class, only the static data member of the class can be called, but not the instance data member of the class, because when a static function member is called, you may not have created a specific instance (object) of the class ).

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.