Re: what does this mean ?
- From: "Doug Harrison [MVP]" <dsh@xxxxxxxx>
- Date: Thu, 7 Jul 2005 11:59:02 -0500
On Fri, 8 Jul 2005 02:37:56 +1000, John Carson wrote:
> "Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote in message
> news:dh8vu1zurpc3.bpeiwb74kp4l$.dlg@xxxxxxxxxx
>> On Thu, 7 Jul 2005 11:23:54 +0300, Alex Blekhman wrote:
>>
>>> Probably, you're confusing it with: new CFoo vs. new CFoo().
>>
>> Now, the question is, "What's the difference?"
>
> By section 5.3.4/15 of the standard, the second form does value
> initialisation, the details of which are described in section 8.5/5.
>
> To illustrate, consider the following as compiled by VC++ 8.0 Beta 2:
>
> struct Test
> {
> int x, y;
> };
>
> int main()
> {
> Test *ptr1 = new Test;
> cout << ptr1->x << ',' << ptr1->y << endl;
>
> Test *ptr2 = new Test();
> cout << ptr2->x << ',' << ptr2->y << endl;
>
> delete ptr1;
> delete ptr2;
> return 0;
> }
>
> x and y are uninitialised in the first case and zero initialised in the
> second case. This is a change that occurred between the 1998 version of the
> standard and the 2003 version.
Don't you mean the 1998 and 2003 versions of VC++? :)
Also, the zero-initialization occurs in the Test() case only if Test is a
POD type, which means, to a fair approximation, a type you can write in C.
In particular, it would not be done were Test defined as:
struct Test
{
int x;
std::string s;
};
While there's no user-defined ctor, the non-static std::string member makes
it a non-POD type, and "new Test()" would leave x uninitialized.
--
Doug Harrison
Microsoft MVP - Visual C++
.
- Follow-Ups:
- Re: what does this mean ?
- From: John Carson
- Re: what does this mean ?
- From: John Carson
- Re: what does this mean ?
- From: Tom Widmer [VC++ MVP]
- Re: what does this mean ?
- References:
- what does this mean ?
- From: Polaris
- Re: what does this mean ?
- From: Igor Tandetnik
- Re: what does this mean ?
- From: Lawrence Groves
- Re: what does this mean ?
- From: Alex Blekhman
- Re: what does this mean ?
- From: Doug Harrison [MVP]
- Re: what does this mean ?
- From: John Carson
- what does this mean ?
- Prev by Date: Re: what does this mean ?
- Next by Date: Is it a bad idea to introduce "Reference" in C++ ?
- Previous by thread: Re: what does this mean ?
- Next by thread: Re: what does this mean ?
- Index(es):
Relevant Pages
|