我是靠谱客的博主 天真长颈鹿,最近开发中收集的这篇文章主要介绍C++中内置变量的初始化,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

对于全局的变量

如果内置类型的变量未被显示地初始化,它的值将由定义的位置决定。

(1).定义在函数体之外的变量将被初始化为0

(2).定义在函数体内部的变量将不被初始化,它的值将是任意的。

 

对于(1)举例如下:

[cpp] view plain copy
  1. short sn;  
  2. int in;  
  3. long ln;  
  4. long long lln;  
  5. float f;  
  6. double d;  
  7. long double ld;  
  8. bool b;  
  9. char c;  
  10. wchar_t wc;  
  11. int nArr[10];  
  12.   
  13. void PrintVariable()   
  14. {  
  15.     cout << "short:" << sn << endl;  
  16.     cout << "int:" << in << endl;  
  17.     cout << "long:" << ln << endl;  
  18.     cout << "long long:" << lln << endl;  
  19.     cout << "float:" << f << endl;  
  20.     cout << "double:" << d << endl;  
  21.     cout << "long double:" << ld << endl;  
  22.     cout << "bool:" << "he" << c << "llo" << endl;  
  23.     cout << "char:" << c << endl;  
  24.     cout << "wchar_t:" << "he" << wc << "llo" << endl;  
  25.     cout << "print array:" << endl;  
  26.     for (int i = 0; i < 10; i ++)  
  27.     {  
  28.         cout << nArr[i] << "    ";  
  29.     }  
  30.     cout << endl;  
  31. }  

结果

short:0
int:0
long:0
long long:0
float:0
double:0
long double:0
bool:he llo
char: 
wchar_t:he0llo
print array:
0    0    0    0    0    0    0    0    0    0 


对于(1)举例如下:

[cpp] view plain copy
  1. void PrintVariable()   
  2. {  
  3.     short sn;  
  4.     int in;  
  5.     int nArr[10];  
  6.   
  7.     int num = in;                   //未被初始化,不请允许拷贝:Run-Time Check Failure #3 - The variable 'in' is being used without being initialized.  
  8.     cout << "short:" << sn << endl;   <span style="white-space:pre">      </span>//未被初始化,不允许访问该成员:Run-Time Check Failure #3 - The variable 'sn' is being used without being initialized.  
  9.     cout << nArr[0] << in << endl;        <span style="white-space:pre">  </span>//未被初始化,不允许访问该成员:Run-Time Check Failure #3 - The variable 'in' is being used without being initialized.  
  10. }  




 

类内的成员变量

如果是在类中定义的类成员,则初始化的顺序为:

1.构造函数初始化

2.如果没有构造函数,则通过类内的初始值进行初始化(可能有些较老的版本不允许有类内初始值)

3.默认初始化(值将是未定义的,是任意的)


没有构造函数初始化:

[cpp] view plain copy
  1. #pragma once  
  2.   
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. class TestData  
  7. {  
  8.   
  9. public:  
  10.     //TestData(void);  
  11.     ~TestData(void);  
  12.   
  13.     void PrintVariable()   
  14.     {  
  15.         cout << "short:" << sn << endl;  
  16.         cout << "int:" << in << endl;  
  17.         cout << "long:" << ln << endl;  
  18.         cout << "long long:" << lln << endl;  
  19.         cout << "float:" << f << endl;  
  20.         cout << "double:" << d << endl;  
  21.         cout << "long double:" << ld << endl;  
  22.         cout << "bool:" << "he" << c << "llo" << endl;  
  23.         cout << "char:" << c << endl;  
  24.         cout << "wchar_t:" << "he" << wc << "llo" << endl;  
  25.         cout << "print array:" << endl;  
  26.         for (int i = 0; i < 10; i ++)  
  27.         {  
  28.             cout << nArr[i] << "    ";  
  29.         }  
  30.         cout << endl;  
  31.     }  
  32. private:  
  33.     short sn;  
  34.     int in;  
  35.     long ln;  
  36.     long long lln;  
  37.     float f;  
  38.     double d;  
  39.     long double ld;  
  40.     bool b;  
  41.     char c;  
  42.     wchar_t wc;  
  43.     int nArr[10];  
  44.   
  45. };  

结果(很可怕):

short:-13108
int:-858993460
long:-858993460
long long:-3689348814741910324
float:-1.07374e+008
double:-9.25596e+061
long double:-9.25596e+061
bool:he蘬lo
char:
wchar_t:he52428llo
print array:
-858993460    -858993460    -858993460    -858993460    -858993460    -858993460
    -858993460    -858993460    -858993460    -858993460


通过构造函数初始化

[cpp] view plain copy
  1. #pragma once  
  2.   
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. class TestData  
  7. {  
  8.   
  9. public:  
  10.     TestData(void) : sn(0), in(0), ln(0), lln(0), f(0), d(0.0), ld(0), b(true), c(' '), wc(L' ')  
  11.     {  
  12.         memset(nArr, 0, 10*sizeof(int));  
  13.     }  
  14.   
  15.     ~TestData(void);  
  16.   
  17.     void PrintVariable()   
  18.     {  
  19.         cout << "short:" << sn << endl;  
  20.         cout << "int:" << in << endl;  
  21.         cout << "long:" << ln << endl;  
  22.         cout << "long long:" << lln << endl;  
  23.         cout << "float:" << f << endl;  
  24.         cout << "double:" << d << endl;  
  25.         cout << "long double:" << ld << endl;  
  26.         cout << "bool:" << "he" << c << "llo" << endl;  
  27.         cout << "char:" << c << endl;  
  28.         cout << "wchar_t:" << "he" << wc << "llo" << endl;  
  29.         cout << "print array:" << endl;  
  30.         for (int i = 0; i < 10; i ++)  
  31.         {  
  32.             cout << nArr[i] << "    ";  
  33.         }  
  34.         cout << endl;  
  35.     }  
  36. private:  
  37.     short sn;  
  38.     int in;  
  39.     long ln;  
  40.     long long lln;  
  41.     float f;  
  42.     double d;  
  43.     long double ld;  
  44.     bool b;  
  45.     char c;  
  46.     wchar_t wc;  
  47.     int nArr[10];  
  48.   
  49. };  


结果:

short:0
int:0
long:0
long long:0
float:0
double:0
long double:0
bool:he llo
char:
wchar_t:he32llo
print array:
0    0    0    0    0    0    0    0    0    0

最后

以上就是天真长颈鹿为你收集整理的C++中内置变量的初始化的全部内容,希望文章能够帮你解决C++中内置变量的初始化所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部