默认变量值 [英] Default variable value

查看:134
本文介绍了默认变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我没有一个值赋给一个变量时,我声明,那么它默认为零或只是任何的记忆是previously?

例如

 浮动X;


解决方案

一个声明的变量可以零初始化 初始化值默认初始化

在C ++标准03 8.5 / 10恰当地定义了每个:

零初始化类型T的对象是指:

- 如果T是一个标量类型(3.9),该对象被设置为0(零)的值变换至T;结果
- 如果T是一个非工会类型,每个非静态数据成员和每个基级子对象搜索
  是零初始化;结果
- 如果T是一个联合类型,对象的第一个指定数据成员初始化为零;结果
- 如果T是数组类型,每个元素都是零初始化;结果
- 如果T是引用类型,则不执行初始化。

默认初始化类型T的对象是指:结果
- 如果T是一个非POD类类型(第9条),T的默认构造函数(和
  是病态的初始化,如果T没有访问的默认构造函数);结果
- 如果T是数组类型,每个元素都是默认初始化;结果
- 否则,该目的是零初始化。

值初始化类型T的对象是指:结果
- 如果T是一个类类型(第9条)与用户声明的构造(12.1),那么默认
  构造T称为(并形成不良的初始化,如果T没有访问
  默认的构造函数);结果
- 如果T是一个非工会类型不用户声明的构造,那么每个非静态
  数据成员和T的基础类组件是值初始化;结果
- 如果T是数组类型,那么每一个元素都是值初始化;结果
- 否则,该目的是零初始化

例如:

 #包括LT&;&iostream的GT;
使用命名空间std;静态int类型的; //零初始化
INT B: //零初始化诠释的main()
{
    INT I; //未定义行为,可能被初始化为任何事情
    静态诠释J; //零初始化    COUT<<\\ nLocal未初始化的int变量[I]<< I<<\\ n;    COUT<<\\ nLocal未初始化的静态int变量[J]。<< J<<\\ n;    COUT<<\\ nGlobal未初始化的静态int变量[A]<< A<<\\ n;    COUT<<\\ nGlobal未初始化的int变量[B]<< B<<\\ n;    返回0;
}

您会注意到变量,结果我将在不同的编译器不同。这样的局部未初始化变量的不应该使用。事实上,如果你打开​​严格编译器警告,编译器将它报告错误。这里是codePAD如何报告存在错误。

  cc1plus:警告被视为错误
在函数'廉政的main():
第11行:警告:'我'在此功能中使用未初始化

编辑:在评论作为理所当然指出的@Kirill V. Lyadvinsky,永远不要是一个相当很强的词,可以有完全有效的code可能使用未初始化的变量他指出,在他的评论的例子。所以,我也许应该说:结果
你不应该使用未初始化的变量,除非你知道自己在做什么。

If I don't assign a value to a variable when I declare it, does it default to zero or just whatever was previously in the memory?

e.g.

float x;

解决方案

A declared variable can be Zero Initialized, Value Initialized or Default Initialized.

The C++03 Standard 8.5/5 aptly defines each:

To zero-initialize an object of type T means:

— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
— if T is a non-union class type, each nonstatic data member and each base-class subobject
is zero-initialized;
— if T is a union type, the object’s first named data member is zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.

To default-initialize an object of type T means:
— if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is an array type, each element is default-initialized;
— otherwise, the object is zero-initialized.

To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized

For example:

#include<iostream>
using namespace std;

static int a; //Zero Initialized
int b; //Zero Initialized

int main()
{
    int i;  //Undefined Behavior, Might be Initialized to anything
    static int j; //Zero Initialized

    cout<<"\nLocal Uninitialized int variable [i]"<<i<<"\n";

    cout<<"\nLocal Uninitialized Static int variable [j]"<<j<<"\n";

    cout<<"\nGlobal Uninitialized Static int variable [a]"<<a<<"\n";

    cout<<"\nGlobal Uninitialized int variable [b]"<<b<<"\n";

    return 0;
}

You will notice The results for variable i will be different on different compilers. Such local uninitialized variables SHOULD NEVER be used. In fact, if you turn on strict compiler warnings, the compiler shall report an error about it. Here's how codepad reports it an error.

cc1plus: warnings being treated as errors
In function 'int main()':
Line 11: warning: 'i' is used uninitialized in this function

Edit: As rightfully pointed out by @Kirill V. Lyadvinsky in the comments, SHOULD NEVER is a rather very strong word, and there can be perfectly valid code which might use uninitialized variables as he points out an example in his comment. So, I should probably say:
You should never be using uninitialized variables unless you know exactly what you are doing.

这篇关于默认变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆