16. c ++ primer 4th notes, static (static) class member

Source: Internet
Author: User

1,StaticNo member functionsThisIt can directly accessStaticMember, but cannot directly use non-StaticMember.

2, Can be called through the scope operator, object, reference, pointerStaticMember.

3, Out-of-classStaticAssign values to member data, or do not need to be specified when defining member functionsStatic.

4BecauseStaticThe Member is not part of any object, soStaticMember functions cannot be declaredConst. After all, declare the member functionConstIt promises not to modify the object to which the function belongs, nor can it be declared as a virtual function.

The best way to ensure that the object is exactly defined once isStaticData member definitions are placed in files that contain non-inline member function definitions.

5IntegerConst staticData members can be initialized in the class definition body.Const staticWhen a data member is initialized in the class definition body, the data member must still be defined outside the class definition body.

6,StaticA member is not a combination of class objects and belongs to a class, but is shared by all objects of a given class.StaticThe data member type can be the class type of the member. NonStaticA member is limited to a pointer or reference to its own class object.

7And each label sets access protection for the members declared between the label and the Next label. The label can appear multiple times in the class.

8 , Const member function (Constant member function), a member function, cannot change the normal object (that is, neither Static Neither Mutable ) Data member. Const Member This Pointer Const Object. Whether a member function can be overloaded depends on whether the function is Const .

# Include <iostream> using namespace STD; Class teststatic {public: teststatic () {cout <"in teststatic" <Endl;} static void output () {cout <"for a test" <Endl; cout <period <Endl;} void output1 () const {cout <"in output1" <Endl ;} void output1 () {cout <"in output1" <Endl;} static int testme; static const int period = 30; static teststatic mem1; // teststatic MEM2; // errorteststatic * mem3;}; // const int teststatic: period; int teststatic: testme = 10; Class teststatic1: Public teststatic {public: teststatic1 () {cout <"in teststatic1" <Endl ;}; int main () {teststatic T1; cout <teststatic: testme <Endl; cout <t1.testme <Endl; t1.output (); teststatic1 T2; cout <t2.testme <Endl; t2.output (); return 1 ;}

 9. access private static data members through public static member functions.

Reference

[1]Http://blog.163.com/zhoumhan_0351/blog/static/39954227201002621251123/

[2] http://blog.163.com/zhoumhan_0351/blog/static/3995422720103179552945/

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.