C ++ study NOTE 2: class member variable Initialization

Source: Internet
Author: User

1. List of constructors available for initialization:

Book: Book (): size (100), weight (50) {}// suppose Size weight is int type class name book

2. If the default constructor (or constructor created by the compiler) is used:

A. if the object is defined in the global scope (not in any function), or is defined as a static local object, the class type is initialized by the constructor, and the member variable of the built-in type is initialized to 0.

B. if the object is defined in a local scope: the class type is initialized by the constructor, and the member variables of the built-in type are not initialized

For example,

Assume that the book class has a member variable of the int type, then:

Int main () {book; cout <book. Size <Endl; // The result shows no initialization}

And:

Int main ()
{
Static book;
Cout <book. Size <Endl; // The result is 0.
}

Or

Book; int main () {book; cout <book. Size <Endl; // The result is 0}



Related Article

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.