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()
yields the value constructed value ofint
(which is I think a C++03 specified thing) and the default value ofint
is0
. This is equivalent toint *ptr = 0;
– CocciNULL
hasn't been allowed to be other values. Source: Stroustrup According to K&RNULL
is always 0, which (in the context of a pointer) is converted to whatever the underlying null-pointer type is. So, in C++,NULL
is 0, irrespective of the underlying hardware. – RupertoNULL
could be a non-zero value. I said it could be any zero-valued integer constant (which includesint()
). – Tournedosint
isn't an object, even if you're using the object instantiation syntax to create one. – Sampsonint
is most certainly an object in C++. – Ehling