Class static member function and const member function

Source: Internet
Author: User

Static member features 

Static member functionsNo this pointer.

Static member functionsCannot be declared as Const.

Static member functionsCannot be declared as a virtual function.

Static member functionsYou can directly access static members of the class, but cannot directly use non-static members.

The type of the static data member. It can be the class type of the member. Non-static members are limited to pointers or references declared as their own class objects.

Static data members can be used as the default real parameters of member functions. A non-static data member cannot be used as the default real parameter of the member function, because its value cannot be used independently of its object.Analysis: If the static member function is not initialized, the system automatically initializes it to a default value (for example, int Initialization is 0, pointer Initialization is null ). Therefore, once defined, its value has been determined and can be used as the default parameter of the class member function.

When defining static members outside the class, you do not need to specify the static keyword again. The static keyword can only be used in the declaration of the class definition body. The definition cannot be marked as static.

 

Static data members must be defined exactly once outside the class definition body. Unlike common data members, static members are not initialized through class constructor, but should be initialized during definition. Generally, the definition of static data members is placed in a file containing non-inline member function definitions of classes.

Generally, static data members are the same as normal data members and cannot be initialized in the class definition body. Static data members are generally initialized during definition. An exception to this rule is that as long as the initialization is a constant expression,The const static data member can be initialized in the class definition body.(Only the const static integer is written on permission'. After testing, it is found that the string can also be used ):

Const static member 1 Class Account {
2 Public :
3 Static   Double Rate (){ Return Interestrate ;}
4 Static   Void Rate ( Double ); // Sets a new rate
5 Private :
6 Static   Const   Int Period =   30 ; // Interest posted every 30 days
7 Double Daily_tbl [period]; // OK: period is constant expression
8 };

 

Primer P400

Access to static data members and member functions

The static member of the category class can be accessed through objects or directly through classes.

Class member functions (whether static member functions or non-static member functions) can directly access static data members. There are two access methods: object. static_var or classname: static_var; object. static_func () or classname: static_func ().

 

Const member functions

normal data members of objects cannot be changed. They can be static and mutable data members.

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.