Static members of a class, as well as constant objects, constant members

Source: Internet
Author: User

One, static member: Static decoration, each class has only one copy, for all of the objects of that class are shared.

1. Static data members:

In-class declarations, out-of-class initialization. As with global variables, allocate memory in the data segment.

eg

Class a{

static int co;

};

int a::co=0;

2. Static member functions:

1). Static member functions are specifically used to access static members of a class, cannot access non-static members, and does not have the this pointer. Declared by static within a class, can be defined outside the class, and no static is required as a prefix at this time.

eg

Class a{

static int co;

static int Getco () {return CO;}

}

2). Static member functions can be called directly through the class name or through an object. The format is: Class Name:: Static member function, object. static member function.

Two, constant object and constant member: const

1. Constant object: Must be initialized by the constructor at the time of definition, only the regular member function can be called, and can only be accessed by the regular member function, the lifetime is not allowed to be modified.

Definition Format: Type const object name, or const type object name.

2. Constant data member: must be initialized in the initialization list of the constructor, not allowed to be modified during lifetime.

Definition Format: Type const data member name, or const type data member name.

3. Constant member function: Cannot update the object's data members (but can be accessed), cannot call the class of the very member functions, in the function declaration and definition must be added to the const keyword.

Definition format: Returns the type member function name (parameter table) Const.

eg

Class a{

int x;

int day () const;

}

int A::d ay () const

{

return x;

}

In addition, const can be used for function overloading.

Eg:void print () and void print () const are two functions that can be overloaded.

Static members of a class, as well as constant objects, constant members

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.