2012年(158)
分类: C/C++
2012-11-23 16:02:09
此文有问题
struct foo
{
int
val;
};
#include
int main( void )
{
using namespace std;
foo* p1 = new foo();
foo* p2 = new foo;
foo* p3 = new foo[2]();
foo* p4 = new foo[2];
cout << p1->val << endl; //
val被初始化为int(),即0
cout << p2->val << endl; // val为随机值
// cout << p3->val << endl; //
val被初始化为int(),即0
// cout << p4->val << endl; // val为随机值
return 0;
}
2008-01-10:
ISO/IEC 14882:2003(E) 中 5.3.4 之 15
— If the
new-initializer is omitted:
— If T is a (possibly cv-qualified) non-POD
class type (or array thereof), the object is default-initialized(8.5). If T is a
const-qualified type, the underlying class type shall have a user-declared
default constructor.
— Otherwise, the object created has indeterminate
value. If T is a const-qualified type, or a (possibly cv-qualified) POD class
type (or array thereof) containing (directly or indirectly) a member of
const-qualified type, the program is ill-formed;
— If the new-initializer is
of the form (), the item is value-initialized (8.5);