Explain the initialization rules for variables in C + + _c language

Source: Internet
Author: User

Objective

When you define a variable that does not have an initialization, the system sometimes helps us initialize the variable.

How the system initializes depends on the type of the variable and the location of the variable definition. 

Whether the built-in type variable is automatically initialized depends on where the variable is defined.

The variables defined outside the function are initially 0; the variables defined in the function body are not automatically initialized. In addition to the left-hand operand used as an assignment, any other behavior that uses an uninitialized variable is undefined and does not rely on undefined behavior.

Take the int type for example, a simple test code:

#include <iostream>
using namespace std;

int A;

int main ()
{
 int b;

 cout << a << Endl;
 cout << b << Endl;

 return 0;
}

In VS execute this code, output variable a value of 0, while vs will be an 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, the default constructor is automatically called for initialization , regardless of where the variable is defined, if no initialization is provided. If a type does not have a default constructor, you must provide a display initializer when defining the type object.

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

#include <iostream>
using namespace std;

Class Testa
{public
 :
 void printf () const
 {
  cout << data << Endl;
 }
 Private:
 int data;

Testa A;

int main ()
{
 testa B;
 
 A.printf ();
 B.printf ();

 return 0;
}

In VS execute this code, get the following results:

The default constructor generated by the compiler initializes the data member using the same rules as the variable initialization. Object A is defined outside the function, and its int type data member is initially 0; object B is defined in the function body, and the composite default constructor is not initialized (conforming to the built-in type variable initialization rules), where all the 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, define a constructor to prevent the compiler from automatically generating the default constructor:

#include <iostream>
using namespace std;

Class Testa
{public
 :
 Testa (int a)
 {
  data = A;
 }

 void printf () const
 {
  cout << data << Endl;
 }
 Private:
 int data;

Testa A;

int main ()
{
 testa B;
 
 A.printf ();
 B.printf ();

 return 0;
}

This code cannot be compiled: error C2512: “testA”: no suitable default constructor is available.

Summarize

The above is about C + + variable initialization rules of the entire content, I hope this article content for everyone to learn to use C + + can help, if there is doubt can message exchange, thank you for the cloud Habitat Community support.

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.