如何“int * ptr = int()”值初始化不是非法? [英] How is "int* ptr = int()" value initialization not illegal?
本文介绍了如何“int * ptr = int()”值初始化不是非法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码(从这里):
int * ptr = int
在Visual C ++中编译,并初始化指针。
这怎么可能?我的意思是 int()
产生一个 int
类型的对象,我不能赋值
$ b < >解决方案
int()
是一个值为0的常量表达式,因此它是一个生成空指针常量的有效方法。最后,它只是一个稍微不同的方式说 int * ptr = NULL;
The following code (taken from here):
int* ptr = int();
compiles in Visual C++ and value-initializes the pointer.
How is that possible? I mean int()
yields an object of type int
and I can't assign an int
to a pointer.
How is the code above not illegal?
解决方案
int()
is a constant expression with a value of 0, so it's a valid way of producing a null pointer constant. Ultimately, it's just a slightly different way of saying int *ptr = NULL;
这篇关于如何“int * ptr = int()”值初始化不是非法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文