The static keyword in C + +

Source: Internet
Author: User

Members in a class can be divided into ordinary and static members, and functions can be divided into ordinary functions and static functions. Ordinary members can only be owned by each object alone, whereas static members are common to all objects of the class. In a static member function, only static member variables can be called, and normal member variables cannot be called.

For example:

1 classMyData2 {3  Public:4MyDataintAintB) {a=a;b=B;}5~MyData () {};6     void StaticFun ()7     {8cout<<"C:"<<c<<endl;//Legal. Static member variables can and can only be called in static functions9         //cout<< "A:" <<a<<endl;//illegal! Normal member variables cannot be called in a static functionTen     } One  A     voidGeta () {cout<<"A:"<<a<<Endl;} -     voidGETB () {cout<<"B:"<<b<<Endl;} -     voidGetC () {cout<<"C:"<<c<<Endl;} the  - Private: -     intA; -     intb; +     Static intC; - }; +  A intMydata::c =5;//a description of the definition of a static member variable at  - intMain () - { -MyData D1 (3,4); -D1.fun ();//invoking a static member function of a class through an object -Mydata::fun ();//calling a static member function of a class through a class name in  -     return 0; to}

The static keyword in C + +

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.