Static and const members of classes in C ++

Source: Internet
Author: User

1. static member variables

The static member variable of the class. After it is declared in the header file, it should be defined in the class source file:

As shown in test. HCode:

 
# Pragma onceclass test {PRIVATE: static int data ;};

The data member variable should be defined in test. cpp:

 

 

 
# Include "test. H" int test: Data = 10;

Note that the code for defining data should not be written in the class constructor, because the constructor will be executed multiple times and should not be placed in the Class header file because the header file may be contained multiple times.

 

2. static member functions

The static member function of a class can be called directly by class name, which is convenient in many cases. However, the static member function of the class can only invoke static member variables and static member functions of the class, because it does not have the this pointer

3. Const member variable

Class const member variables can only be initialized in the const member initialization list of the constructor, and cannot be modified later. You can set some read-only variables of the class to const.

Test. h file:

 

# Pragma onceclass test {PRIVATE: const int data ;};

Test. cpp file:

 

 

 
# Include "test. H" test: Test (void): Data (10 ){}

 

4. Const member functions

The const member function of the class, which has the const this pointer. Therefore, you cannot change the member variables in the class. However, this is a special case. When the member variable is declared as mutable, it can be changed by the const member function of the class.

If the class member function does not change the class data, it should be defined as a const member function. First, it makes the class interface easier to understand and tells the caller, this function does not change the data of the class. Secondly, it can expand the effective usage scope of this function:

For example, the test class has a function that prints the basic information of the class. This function is read-only for the class data. If it is not set to const, the const test instance cannot call this function, which is not logically reasonable. So we should set it as a member function of Const. In this way, both the instance of test and the instance of const test can call this function.

5. Static const member variables

Static const member variables are also similar to static member variables after they are declared in the Class header file. You should define them in the class source file:

Test. h file:

 

# Pragma onceclass test {PRIVATE: static const int data ;};

The data member variable should be defined in test. cpp:

 

 

 

 
# Include "test. H" const int test: Data = 10;

However, for int-Type Static const member variables, you can define them directly when declaring them, as shown below:

 

 
# Pragma onceclass test {PRIVATE: static const int DATA = 10 ;};

In this way, data does not need to be defined in test. cpp. But this is only applicable to the int type. If you replace int with float or another one, the compilation will fail. I don't know why.

 

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.