博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++中内置变量的初始化
阅读量:7138 次
发布时间:2019-06-28

本文共 3535 字,大约阅读时间需要 11 分钟。

hot3.png

对于全局的变量

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

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

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

 

对于(1)举例如下:

short sn;int in;long ln;long long lln;float f;double d;long double ld;bool b;char c;wchar_t wc;int nArr[10];void PrintVariable() {	cout << "short:" << sn << endl;	cout << "int:" << in << endl;	cout << "long:" << ln << endl;	cout << "long long:" << lln << endl;	cout << "float:" << f << endl;	cout << "double:" << d << endl;	cout << "long double:" << ld << endl;	cout << "bool:" << "he" << c << "llo" << endl;	cout << "char:" << c << endl;	cout << "wchar_t:" << "he" << wc << "llo" << endl;	cout << "print array:" << endl;	for (int i = 0; i < 10; i ++)	{		cout << nArr[i] << "    ";	}	cout << endl;}
结果

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)举例如下:

void PrintVariable() {	short sn;	int in;	int nArr[10];	int num = in;					//未被初始化,不请允许拷贝:Run-Time Check Failure #3 - The variable 'in' is being used without being initialized.	cout << "short:" << sn << endl;			//未被初始化,不允许访问该成员:Run-Time Check Failure #3 - The variable 'sn' is being used without being initialized.	cout << nArr[0] << in << endl;			//未被初始化,不允许访问该成员:Run-Time Check Failure #3 - The variable 'in' is being used without being initialized.}

 

类内的成员变量

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

1.构造函数初始化

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

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

没有构造函数初始化:

#pragma once#include 
using namespace std;class TestData{public: //TestData(void); ~TestData(void); void PrintVariable() { cout << "short:" << sn << endl; cout << "int:" << in << endl; cout << "long:" << ln << endl; cout << "long long:" << lln << endl; cout << "float:" << f << endl; cout << "double:" << d << endl; cout << "long double:" << ld << endl; cout << "bool:" << "he" << c << "llo" << endl; cout << "char:" << c << endl; cout << "wchar_t:" << "he" << wc << "llo" << endl; cout << "print array:" << endl; for (int i = 0; i < 10; i ++) { cout << nArr[i] << " "; } cout << endl; }private: short sn; int in; long ln; long long lln; float f; double d; long double ld; bool b; char c; wchar_t wc; int nArr[10];};
结果(很可怕):

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

通过构造函数初始化

#pragma once#include 
using namespace std;class TestData{public: TestData(void) : sn(0), in(0), ln(0), lln(0), f(0), d(0.0), ld(0), b(true), c(' '), wc(L' ') { memset(nArr, 0, 10*sizeof(int)); } ~TestData(void); void PrintVariable() { cout << "short:" << sn << endl; cout << "int:" << in << endl; cout << "long:" << ln << endl; cout << "long long:" << lln << endl; cout << "float:" << f << endl; cout << "double:" << d << endl; cout << "long double:" << ld << endl; cout << "bool:" << "he" << c << "llo" << endl; cout << "char:" << c << endl; cout << "wchar_t:" << "he" << wc << "llo" << endl; cout << "print array:" << endl; for (int i = 0; i < 10; i ++) { cout << nArr[i] << " "; } cout << endl; }private: short sn; int in; long ln; long long lln; float f; double d; long double ld; bool b; char c; wchar_t wc; int nArr[10];};

结果:

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

转载于:https://my.oschina.net/verynix/blog/365842

你可能感兴趣的文章
Unix(AIX) set命令
查看>>
spring使用@Value标签读取.properties文件的中文乱码问题的解决
查看>>
Oracle 11gR2 RAC监听器原理介绍
查看>>
Oracle HA 之 测试RAC的功能
查看>>
CentOS7 Failed to start LSB: Bring up/down
查看>>
关于程序猿的几个阶段!
查看>>
Linux内核中断处理体系分析
查看>>
FlatBuffers要点
查看>>
jquery给input标签添加data-options属性
查看>>
CALayer & bitmap Content
查看>>
openstack_swift源代码分析——Swift单机部署
查看>>
创业建议干货分享
查看>>
5个经常使用的开源聊天应用
查看>>
Android开发中string.xml文件的使用
查看>>
springboot-21-maven多环境打包
查看>>
PhoneGap3+版本号的安装、配置和使用[图]
查看>>
P1118 [USACO06FEB]数字三角形Backward Digit Su…
查看>>
FEC之我见一
查看>>
使用JPA中@Query 注解实现update 操作
查看>>
判断一个枚举值是否属于某个枚举类
查看>>