Introduction to the definition and application of static data members in C ++

Source: Internet
Author: User

The C ++ programming language has been around for 20 years since its appearance. In the past 20 years, it has played a very important role in the development field by virtue of its unique performance and flexible application methods. Today, we will analyze the characteristics of this language from the C ++ static data member.

  • Analysis of basic implementation methods of C ++ Using Interfaces
  • Analysis of the similarities and differences between the left and right values of C ++
  • C ++ template parameters
  • C ++ template function reloads different comments
  • Interpretation of c ++ pointer-heavy application code

When the static keyword is added before the declaration of the data member in the class body, the data member becomes the static data member of the class. Like other data members, static data members also comply with public/protected/private access rules. Static data members also have the following features:

1. Definition of C ++ static data members.

Static data members are actually global variables in the category. Therefore, the definition (initialization) of static data members should not be put in the header file.

The definition is the same as that of global variables. Example:

Xxx. h file

 
 
  1. Class base {
  2. Private:
  3. Static const int _ I; // declaration, standard c ++ supports sequential type initialization in the class body, but vc6 does not.
  4. };

Xxx. cpp File

 
 
  1. Const int base: _ I = 10; // when defining (initialization), private and protected access is not restricted.

Note: do not try to define (initialize) static data members in the header file. In most cases, this will lead to repeated definitions of such errors. Not even if # ifndef # define # endif or # pragma once is added.

2. The C ++ static data member is shared by all objects of the class, including objects of the class derived class. That is, the derived class Object shares the static data member of the base class with the base class object. Example:

 
 
  1. Class base {
  2. Public:
  3. Static int _ num; // Declaration
  4. };
  5. Int base: _ num = 0; // real definition of static data members
  6. Class derived: public base {
  7. };
  8. Main ()
  9. {
  10. Base;
  11. Derived B;
  12. A. _ num ++;
  13. Cout <"base class static data number _ num is" <a. _ num <endl;
  14. B. _ num ++;
  15. Cout <"derived class static data number _ num is" <B. _ num <endl;
  16. }
  17. // The result is 1 or 2. The derived class and the base class share a static data member.

3. The C ++ static data member can be an optional parameter of the member function, but not a common data member. Example:

 
 
  1. Class base {
  2. Public:
  3. Static int _ staticVar;
  4. Int _ var;
  5. Void foo1 (int I = _ staticVar); // correct. _ staticVar is a static data member.
  6. Void foo2 (int I = _ var); // error. _ var is a common data member.
  7. };

4. the type of the C ++ static data member can be the type of the class to which it belongs, but the type of the common data member cannot. A common data member can only be declared as a pointer or reference of the class type. Example:

 
 
  1. Class base {
  2. Public:
  3. Static base _ object1; // correct, static data member
  4. Base _ object2; // Error
  5. Base * pObject; // correct, pointer
  6. Base & mObject; // correct, reference
  7. };

5. I don't know whether it is a feature in Standard c ++ or a feature of vc6. The value of the C ++ static data member can be legally changed in the const member function. Example:

 
 
  1. Class base {
  2. Public:
  3. Base () {_ I = 0; _ val = 0 ;}
  4. Mutable int _ I;
  5. Static int _ staticVal;
  6. Int _ val;
  7. Void test () const {// const member function
  8. _ I ++; // correct, mutable data member
  9. _ StaticVal ++; // correct, static data member
  10. _ Val ++; // Error
  11.  
  12. }
  13. };
  14. Int base: _ staticVal = 0;

Related Article

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.