Initialization of static members, static const members, and const members in C ++

Source: Internet
Author: User

(1) Static members should be initialized outside the class: such members belong to the entire class, but they are very large, so they can be initialized outside the class, or they can be initialized using static functions.


Class A <br/>{< br/> PRIVATE: <br/> static int A; <br/>}; <br/> int A: a = 0; 

 

(2) The const static member can be initialized within the class: such a member is a constant of the entire class.


Class B <br/>{< br/> PRIVATE: <br/> const static int B = 3; <br/> }; 


(3) const members should be initialized in the constructor initialization table: such members are contained in each object, but only constants. Therefore, they must be initialized each time an object is constructed.


Class C <br/>{< br/> Public: <br/> const int size; <br/> C (INT size) <br/>{< br/> // codes <br/>}< br/>}; <br/> C: C (INT size), size (size) <br/>{< br/> // codes <br/>} 

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.