Basic knowledge of C + +---Initialization of static const member variables

Source: Internet
Author: User

In order to limit the scope of a constant to class, you must make it a member of class, and to make sure that there is only one entity for this constant, you must make it a static member:

Class Gameplayer

{

Private:

staticconst int number = 5; Constant-declarative

Intscores[number]; Use this constant

};

What you see, however, is the declaration of number and not the definition of the formula. Usually C + + requires you to provide a definition for anything you use, but if he is a class-specific constant that is static and is of integer type, special handling is required. As long as they do not take their addresses, you can declare and use them without having to provide a defined formula. But if you take the address of a class-specific constant, or even if you don't take its address and your compiler insists on seeing a definition, you have to provide a different definition as follows:

Const int gameplayer::number; The definition of number

Please put this formula into an implementation asking price rather than a header file. Since the class constant has an initial value at the time of declaration, it is not possible to set the initial value in the definition.

For the use of the Const keyword in an STL iterator:

The STL iterator is modeled as a pointer, so the iterator acts like a t* pointer. Declaring an iterator as const is like declaring a pointer as const (that is, declaring a t* const pointer), which means that the iterator must not point to something different, but what it refers to can be changed. If you want the iterator to be something you can't change (you want the STL to emulate a const t* pointer), you need to be const_iterator;

Std::vector<int> VEC;

Const Vector<int>::iterator iter =vec.begin (); ITER acts like a t* const.

*iter = 10; OK

++iter;//, you can't.

Vector<int>::const_iterator citer =vec.begin ();

*citer = 10; Error

++citer;//can

Cost member function

The purpose of the Const scope member function is to confirm that the member function can be used on a const object.

C + + has a very fixed "member initialization order". The order is always the same: base classes is initialized earlier than its derived classes, and class's member variables are always initialized in their declaration order. Even if their members appear in different order in the initial column, there is no effect. When you bar individual members in a member's initial value column, it is best to always order them in the order in which they are declared.

Constructors are best used with member initial values, rather than using assignment operations within the constructor body. The initial Value column lists the member variables that should be sorted in the same order as they are declared in class.

When is empty class no longer a null? After C + + has processed it. If you do not declare yourself, the compiler will declare it (compiler Version) a copy constructor, a copy assignment operator, and a destructor. In addition, if you do not declare any constructors, the compiler will also declare a default constructor for you. All of these functions are public and inline. At the same time, only if these functions are needed will they be compiled and created. If any constructors are customized by the user, the compiler will not synthesize a default constructor

If you intend to support assignment operations within a class containing reference members, you must define the copy assignment operator yourself. In the face of classes with const members, the compiler is also unassigned, and custom copy assignment is required. If a base classes declares the copy assignment operator as Privaete, the compiler rejects it derived Class generates a copy assignment operator.

So it's better to define these functions yourself, and if you don't want to use the compiler's auto-generated functions, you should explicitly deny

The assignment (Assignment) operator returns a reference to *this

Basic knowledge of C + +---Initialization of static const member variables

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.