Static data member of the class

Source: Internet
Author: User

Static data members of a class have different meanings from common static variables. They mean that each object of A Class shares a bucket instead of a bucket, all objects in this class can directly access the bucket. In fact, it is a variable dedicated for the object of this class-if you declare it as private permission.
To define static data members in a class, you only need to add "static" before the definition ". Static data members of a class can only be initialized outside the class. If they are not initialized, the class is automatically initialized to 0. Static data members referenced outside the class must always be in the form of class name: variable name. Static data members can be used to count how many such objects are created.
For example:
# Include <iostream>
Using namespace STD;
Class AA
{
PRIVATE:
Int;
Public:
Static int count; // defines static members of a class.
AA (int aa = 0) {A = AA; count ++ ;}
// For the class method, if it is relatively simple, you can write it to makeProgramCompact
Int get_a () {return ;}
};

Int AA: Count = 0; // initialization outside the class

Void main ()
{
Cout <"Count =" <AA: Count <Endl;
Aa x (10), y (20 );
Cout <"X. A =" <X. get_a () <"Count =" <X. Count <Endl;
Cout <"Y. A =" <Y. get_a ();
}

We can also define static functions. You only need to add static functions to the front when declaring the function, but do not need to add static functions when defining the function. When using static functions, you also need the form of "Class Name: static member function name (parameter table.

 

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.