int a = int();在C ++ 98中会发生什么? [英] int a=int(); what happens in C++98?
问题描述
在您认为将其标记为重复之前,请先阅读该问题。
int i = int();
大多数程序员会说,这里有值初始化&我将值初始化。 (0作为输出)。但它也打印0作为C ++ 98编译器的输出。
下面的程序,我测试了C ++ 98实现,并给我0作为输出。
#include< iostream>
int main()
{
int i = int();
std :: cout<< i;
}
不要说我是在C ++ 98程序中初始化的值,因为值初始化在C ++ 03中引入。所以我是如何初始化这里?是真的构造函数调用吗? int()看起来像构造函数调用。原语类型还具有C ++中的默认构造函数,如Bjarne stroustrup在他的书C ++编程语言& TC ++ PL。
C ++编程语言Bjarne stroustrup:
10.4.2内置类型也有默认构造函数
也读取同一书的第6.2.8节。
以下链接也说在C ++中有默认构造函数。
1) http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=15
2) http://www.geeksforgeeks.org/c-default-constructor-built-in-types/ < a>
那么我真的可以说它是整数类型的构造函数调用。
5.2.3显式类型转换(功能符号)
2表达式
T()
,其中T
是非数组完整对象类型的简单类型说明符(7.1.5.2)或(可能是cv限定的)void类型,创建指定类型的右值,其值由
default-initialization(8.5;没有为void()
情况进行初始化。 [...]
8.5初始化要默认初始化类型
T
的对象意味着:
code> T 是非POD类类型(第9节),
T
的默认构造函数被调用如果T
没有可访问的默认构造函数,初始化是错误的);
- 如果
T
是一个数组类型,每个元素都是默认初始化的;
- 否则,已初始化。
没有问题。 int()
已经保证从第一个C ++标准评估为零。事实上,它是通过默认初始化,而不是价值初始化,是一个技术细节,是完全不相关的你的问题。
Please read the question entirely before you think to mark it as duplicate. The statement like
int i=int();
most programmers will say that there is value initialization here & i will be value initialized. (0 as output). But it also prints 0 as output on C++98 compiler. Following program that I tested on C++98 implementation and gives me 0 as output.
#include <iostream>
int main()
{
int i=int();
std::cout<<i;
}
Don't say that i is value initialized in above C++98 program ,because value initialization introduced in C++03. So How i is initialized here? Is it really constructor call? int() looks like constructor call. Primitive types have also default constructors in C++ as said by Bjarne stroustrup in his book C++ programming language & TC++PL. The C++ programming language Bjarne stroustrup:
10.4.2 Built in types also have default constructors
also read section 6.2.8 of same book.
The following links also says that built in types have default constructors in C++.
1) http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=15
2) http://www.geeksforgeeks.org/c-default-constructor-built-in-types/
So can I really say that it is constructor call of integer type?
5.2.3 Explicit type conversion (functional notation)
2 The expression
T()
, whereT
is a simple-type-specifier (7.1.5.2) for a non-array complete object type or the (possibly cv-qualified) void type, creates an rvalue of the specified type, whose value is determined by default-initialization (8.5; no initialization is done for thevoid()
case). [...]8.5 Initializers
5 [...] To default-initialize an object of type
T
means:-- if
T
is a non-POD class type (clause 9), the default constructor forT
is called (and the initialization is ill-formed ifT
has no accessible default constructor);-- if
T
is an array type, each element is default-initialized;-- otherwise, the storage for the object is zero-initialized.
There is no problem. int()
has been guaranteed to evaluate to zero right from the very first C++ standard. The fact that it happened through default-initialization, rather than value-initialization, is a technical detail that is completely irrelevant for your question.
这篇关于int a = int();在C ++ 98中会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!