C + + variable initialization rules

Source: Internet
Author: User

When defining a variable without initialization, the system sometimes helps us initialize the variable. How the system is initialized depends on the type of the variable and where the variable is defined .

Whether the built-in type variable is automatically initialized depends on where the variable is defined . The variables defined in the function body are initially 0, and the variables defined in the function body are not initialized automatically. In addition to the left operand used as an assignment operation, any other behavior that uses uninitialized variables is undefined and does not depend on undefined behavior.

Take the int type as an example, a simple test code:

#include <iostream>usingnamespace  std; int A; int Main () {    int  b;     << a << Endl;     << b << Endl;     return 0 ;}
View Code

In VS executes this code, output variable A's value 0, while vs will error: Run-time Check Failure #3-the variable ' b ' is being used without being initialized. Variable A is automatically initialized to 0, and variable b is not automatically initialized.

  When a class-type variable is defined, if no initialization is provided, the default constructor is automatically called for initialization (regardless of where the variable is defined). If a type does not have a default constructor, you must provide a display initializer when you define the object of that type.

A simple test code (the default constructor is generated automatically by the compiler):

#include <iostream>using namespacestd;classtesta{ Public:        voidprintf ()Const{cout<< Data <<Endl; }    Private:        intdata;}; TestA A;intMain () {TestA B;    A.printf ();    B.printf (); return 0;}
View Code

In vs execution of this code, the following results are obtained:

The compiler automatically generates a default constructor that initializes the data member with the same rules as the variable initialization. Object A is defined outside the body of the function, its int type data member is initially 0, and object B is defined in the function body, and the composition default constructor does not initialize it (conforming to the built-in type variable initialization rules), where random values are stored. Similarly, if the data member is a class type, the corresponding default constructor is called to initialize the data member.

If you change the definition of this class A little bit, define a constructor to prevent the compiler from automatically generating the default constructor:

#include <iostream>using namespacestd;classtesta{ Public: TestA (inta) {data=A; }        voidprintf ()Const{cout<< Data <<Endl; }    Private:        intdata;}; TestA A;intMain () {TestA B;    A.printf ();    B.printf (); return 0;}
View Code

This code cannot be compiled: Error C2512: "TestA": No appropriate default constructor is available.

C + + variable initialization rules

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.