Examples of static and const usages in C + + classes _c language

Source: Internet
Author: User

Static and const are very important concepts in C + + programming, and the examples in this article enumerate the rules and usages of static and const in C + + classes. For reference. The details are as follows:

First, use the code to illustrate. The sample code is as follows:

Class A
{public
:
  A (): M (//const)         member must initialize
  {
    q = n

  in the constructor's initialization list) void Fun1 () const
  {
    m++;         Error. A const member is a constant and cannot change its value.
    n++;         That's right. The static variable n belongs to the class, but each object's function can access and change it.
    q++;         Error. A const member function cannot change the value of a data member.
  }
  static void Fun2 ()
  {
    m++;         Error. A const member is a constant and cannot change its value.
    n++;         That's right. A static member function can access and change the value of a static variable.
    q++;         Error. A static member function cannot access and change the value of a non-static data member.
  }

  const int m;
  static int n;
  static const int p;
  int q;
};

int a::n = 5;        The static member must be initialized outside of the class without the keyword static, but to specify class scope a::
const int A::P =;     The static const member is initialized outside the class like a static member (rather than in the constructor initialization list), and remember to add the keyword const

The following is a detailed description.

One, static key word

1.static data member

A static data member is a class, is not part of any specific object, and does not occupy the memory space of the object. can be accessed in the form of a::n, or through object access (although not a specific object, but all objects are common).

The initialization of the static data member must be initialized outside the class, with int a::n = 5; In this form, remember to indicate the type and the class to which it belongs, without adding the keyword static.

2.static member function

A static member function can only access static data members or static member functions and cannot access non-static data members and non-static member functions.

Two, the const keyword

1.const data member

Must be initialized in the constructor initialization list. The reason can be understood to have the following two points.

(1) A member of a class cannot initialize a declaration, such as int c = 3 in a class body;

(2) You cannot assign a value in a member function because the const cannot be changed.

2.const member function

The const member function can access all data members but not the value of any one of the data members of the object, but can change the value of the static member (the static member belongs to the class and not to the specific object)

3.const objects

const a A;

A const object can only invoke a const member function and can only change a static member.

Three, static const keyword

The first thing to remember is that the static const representation is both static and const, both of which are characteristic.

The static const int p;//and the const static int p;

Initialize like a static member, outside of a class, but with a const.

const int A::P = 30;

I hope this article is helpful to the object-oriented programming of C + +.

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.