Re: new X vs new X()
From: Rob Williscroft (rtw_at_freenet.co.uk)
Date: 06/22/04
- Next message: Peter Koch Larsen: "Re: swap two integers without using a tmp variable?"
- Previous message: Peter Koch Larsen: "Re: swap two integers without using a tmp variable?"
- In reply to: Victor Bazarov: "Re: new X vs new X()"
- Next in thread: JKop: "Re: new X vs new X()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 22 Jun 2004 16:07:03 GMT
Victor Bazarov wrote in news:UCXBc.1476$ri.119739@dfw-read.news.verio.net
in comp.lang.c++:
>> Nowadays its value-initialized, which is a kind-of recursive
>> default-intialization.
>>
>> In 8.5/5
>>
>> 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;
>>
>> 8.5/7
>>
>> An object whose initializer is an empty set of parentheses,
>> i.e., (), shall be value-initialized.
>
> Thanks, Rob. So, in the case of 'new X', if X is a POD it is still
> _uninitialised_. The only difference now is that it's not "default-
> initialised" for PODs but "value-initialised", right?
>
> You don't have to reply, I'm just repeating here what Andrew Koenig's
> reply stated (I hope).
>
Ok :), but I will add that value-initialization isn't just for POD's,
it applies to any "non-union class type" without used defined
constructors.
#include <iostream>
#include <ostream>
#include <iomanip>
#include <vector>
struct X
{
std::vector< int > vi;
int x;
};
struct Y
{
std::vector< int > vi;
X x;
};
struct Z
{
std::vector< int > vi;
Y y;
};
void messitup()
{
int array[ 1000 ];
for ( int i = 0; i < 1000; ++i )
{
array[ i ] = 0xCCCCCCC;
}
}
bool is_vi()
{
Z z = Z();
return z.y.x.x == 0;
}
bool is_new_vi()
{
Z *z = new Z();
return z->y.x.x == 0;
}
int main()
{
messitup();
std::cout
<< "value-initialization: "
<< std::boolalpha << is_vi()
<< std::endl
;
std::cout
<< "fake-value-initialization: "
<< std::boolalpha << is_new_vi()
<< std::endl
;
}
1 out of 4 of my compilers returned true, true, 1 returned false, true.
I'm a bit dissapointed gcc 3.4 doesn't support this.
Rob.
-- http://www.victim-prime.dsl.pipex.com/
- Next message: Peter Koch Larsen: "Re: swap two integers without using a tmp variable?"
- Previous message: Peter Koch Larsen: "Re: swap two integers without using a tmp variable?"
- In reply to: Victor Bazarov: "Re: new X vs new X()"
- Next in thread: JKop: "Re: new X vs new X()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|