Initialization and features of static members in the C ++ class

Source: Internet
Author: User

Initialization and features of static members in the C ++ class

Some member variables in the C ++ class are initialized differently from those in the general data type. The following test compilation environment is:

➜   g++ -vUsing built-in specs.COLLECT_GCC=g++Target: x86_64-linux-gnugcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 

Test code (compiled by g ++ ):

# Include <iostream> using namespace std; class Test {public: Test (): y (1), r (y), d (3) {} // for constant and referenced member variables, initialization must be performed through the parameterized list. ~ Test () {} int y; // normal variable member int & r; // reference member variable const int d; // constant member variable static int c; // static member variable static const int x = 2.1; // static constant integer member variable static const int xx; // static constant integer member variable declaration static const double z; // static constant non-integer member variable declaration static const float zz = 6.6; // static constant non-integer member variable}; const int Test: xx = 4; // static constant integer member variable definition const double Test: z = 5.1; // static constant non-integer member variable definition int Test: c = 2; int main (void) {cout <Test: x <endl; return 0 ;}

These special types of member variables mainly include:

1. Reference 2. Constant 3. static variable 4. Static integer constant 5. Static non-integer constant
  • For = 1. Reference = and = 2. Constant =, the member variables must be initialized by using the constructor's = parameter list =. For example, the r and d variables in the above program are initialized.

  • For = 3. static variables =. static member variables need to be initialized and defined in the class definition body. Because static data members are independent of any objects in the class, they are the objects associated with the class, it is not associated with class objects. For example, the initialization of c variables in the above program.

  • For = 4. Static integer constant =, this type of member can be initialized directly in the class or declared in the class and defined in the class definition. For example, the x and xx variables in the above program.

  • For static non-Integer constants = 5, this type can also be declared in the class definition in vitro or initialized directly in the class definition. For example, the z and zz variables in the above program.

Conclusion:

Static members belong to the class scope, but do not belong to class objects. their lifecycles are the same as normal static variables. They are allocated and initialized when the program is running, and released when the program ends. Therefore, initialization cannot be performed in class constructors.

Advantages of static members
  • The static member name is in the scope of the class, so it can avoid conflicts with other class members or global object names.
  • Encapsulation can be implemented. static members can be private, but global objects cannot.
  • The reader can easily see that static members are associated with a class, which clearly reflects the programmer's intent.
Static member functions
  • Because the static member function does not have the this pointer, the static member function cannot access non-static members.
  • Non-static member functions can access static members.
  • Static data members are irrelevant to the class size, because static members only apply to the class range.
# Include <iostream> using namespace std; class test2 {public: test2 (int num): y (num ){}~ Test2 () {} static void testStaticFun () {cout <"y =" <y <endl; // Error: static member functions cannot access non-static members} void testFun () {cout <"x =" <x <endl;} private: static int x; // description of static member variables int y;}; int test2: x = 10; // definition of static member variables int main (void) {test2 t (100); t. testFun (); return 0 ;}
Static usage Summary
  • C language:
    • It is used to modify internal variables of a function, that is, static variables in the function. The lifetime of this variable is longer than that of this function, so that the function has a certain "State ". Functions that use static variables are generally not reentrant, nor thread-safe, such as strtok (3 ).
    • When a variable or function is modified at the file level (outside the function body), it indicates that the variable or function is visible only in this file. Other files cannot see or access the variable or function. The professional statement is "having internal linkage" (in short, it is not exposed to other translation units ).
  • C ++ language (because C ++ introduces classes, static keywords have two new usage methods while maintaining compatibility with c language ):
    • It is used to modify the data members of a class, that is, "static members ". This type of data member has a lifetime greater than the class Object (instance/instance ). A static data member has one copy for each class, and a common data member has one copy for each instance.
    • The member function used to modify the class, that is, the so-called "static member function ". This type of member function can only access static members and other static programmer functions, but cannot access non-static members and non-static member functions.

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.