Is there any difference between:
A* p = new A;
and
A* p = new A();
?
I know it must have been dicussed here, but just
not easy to search. BTW, I think
A a;
is alike new A as far as constructor call concerned
(default construtor call?) Right?
And
A a();
is just a function declaration?
Thanks and best regards,
Wenjie
There is no difference for classes, there is a difference
for POD: the former leaves the object uninitialised, the
latter zero-initialises it.
> I know it must have been dicussed here, but just
> not easy to search. BTW, I think
> A a;
> is alike new A as far as constructor call concerned
> (default construtor call?) Right?
It's a declaration (and a definition in certain circumstances)
of an object 'a' of type A. If it's a definition, the object
is default-initialised, which for a class means that its c-tor
is invoked, yes.
>
> And
> A a();
> is just a function declaration?
Yes, it is.
Victor
Wenjie wrote:
>
> Hello,
>
> Is there any difference between:
> A* p = new A;
> and
> A* p = new A();
> ?
Yes, there is a difference in certain circumstances.
It has to do with POD's (POD = plain old data structure,
basically a class or a struct built from only builtin types
and having no member functions, in short: a struct as it was
ment to be in C)
new A
creates a new POD-object of type A and leaves all members uninitialized
new A()
creates a new POD-object of type A but 0-initializes all members.
> A a();
> is just a function declaration?
Yep. That's a pitfall.
--
Karl Heinz Buchegger
kbuc...@gascad.at
> There is no difference for classes,
non-POD classes (but you knew that).
That's not the definition of POD.
POD classes can't have
non-static data of non-pod type (other non-POD structs or unions, pointer-to-members or references).
user-defined constructors, destructors, or copy-assignment operators
base classes
protected or private non-static data members
virtual functions.
They can however have other member functions not specifically mentioned above.
In short, a class whose data behaves identically to a C struct.
The default constructor (whether user defined or not) is
called in both cases like for a definition "A a;" right?
Then "zero-init" is only for simplifying the anwser, since
what I know a default constructor could be written as:
class A {
public:
A(): a_(12) {}
//...
private:
int a_;
};
>
> > I know it must have been dicussed here, but just
> > not easy to search. BTW, I think
> > A a;
> > is alike new A as far as constructor call concerned
> > (default construtor call?) Right?
>
> It's a declaration (and a definition in certain circumstances)
> of an object 'a' of type A. If it's a definition, the object
> is default-initialised, which for a class means that its c-tor
> is invoked, yes.
>
> >
> > And
> > A a();
> > is just a function declaration?
>
> Yes, it is.
>
>
> Victor
Thanks,
If it's not defined ("trivial"), how would it be called?
>
> Then "zero-init" is only for simplifying the anwser, since
> what I know a default constructor could be written as:
Zero-initialisation is only performed for POD. As soon as
you define a constructor, it's not a POD any longer.
>"Wenjie" <gok...@yahoo.com> wrote...
>> "Victor Bazarov" <v.Aba...@attAbi.com> wrote in message
>news:<vfm231m...@corp.supernews.com>...
>> > "Wenjie" <gok...@yahoo.com> wrote...
>> > > Is there any difference between:
>> > > A* p = new A;
>> > > and
>> > > A* p = new A();
>> > > ?
>> >
>> > There is no difference for classes, there is a difference
>> > for POD: the former leaves the object uninitialised, the
>> > latter zero-initialises it.
This is all fine. I just got into a heated debate with a colleague
about operator new[] though:
Q1. Is operator new[] allowed to take a non-default constructor for
a user-defined type? Example:
class A { public: A(int, int, int); };
A* p = new A[100](1, 2, 3);
My position is yes. GCC 3.2 apparently agrees.
Q2. For a POD type, is operator new[] allowed to take non-default
initializers? Example:
int* p = new int[100](1);
My inclination is to say yes. But GCC is not on my side this time.
For int* p = new int(1), *p is initialized to 1. With operator new[]
though, it did not.
Q3. For a POD type, should the following two statements both perform
zero initialization:
int *p = new int[100];
int *p = new int[100]();
My understanding is the first one doesn't and the second one does, just
like for operator new.
We've consulted the standard but our interpretations are different.
My basic stance is what's quoted for operator new should apply as well to
operator new[].
--Yu
If the user-defined type does not have a default constructor,
the program that tries to dynamically allocate an array of such
type is ill-formed. See Standard
> Example:
>
> class A { public: A(int, int, int); };
> A* p = new A[100](1, 2, 3);
>
> My position is yes.
The Standard doesn't agree with you. See 5.3.4/15. Only default
initialisation is allowed for arrays.
> GCC 3.2 apparently agrees.
Doesn't prove anything.
>
> Q2. For a POD type, is operator new[] allowed to take non-default
> initializers?
Nope.
> Example:
>
> int* p = new int[100](1);
>
> My inclination is to say yes.
Oh, well...
> But GCC is not on my side this time.
> For int* p = new int(1), *p is initialized to 1. With operator new[]
> though, it did not.
Right. PODs are left uninitialised.
>
> Q3. For a POD type, should the following two statements both perform
> zero initialization:
>
> int *p = new int[100];
> int *p = new int[100]();
The first one does not perform any initialisation.
>
> My understanding is the first one doesn't and the second one does, just
> like for operator new.
>
> We've consulted the standard but our interpretations are different.
> My basic stance is what's quoted for operator new should apply as well to
> operator new[].
It doesn't. But if you'd like to make a suggestion, you should
definitely do so in comp.std.c++.
Victor
> > The default constructor (whether user defined or not) is
> > called in both cases like for a definition "A a;" right?
>
> If it's not defined ("trivial"), how would it be called?
>
As far as the standard is concerend it behaves as if one was automatically
inserted by the compiler.
> Q1. Is operator new[] allowed to take a non-default constructor for
> a user-defined type? Example:
>
> class A { public: A(int, int, int); };
> A* p = new A[100](1, 2, 3);
It's got nothing to do with operator new[] Operator new[] is the allocation
function. What you have above is illegal C++ syntax involving the new[]
operator. One of the screwed up things in C++ is the naming of the allocation
function.
>
> My position is yes. GCC 3.2 apparently agrees.
Your position is wrong and GCC is as well. Try turning on the "disabled fucked-up
GNU extensions flag." (-pedantic-errors)
>
> Q2. For a POD type, is operator new[] allowed to take non-default
> initializers? Example:
>
> int* p = new int[100](1);
What you wrote above is illegal for ANY type.
>
> Q3. For a POD type, should the following two statements both perform
> zero initialization:
>
> int *p = new int[100];
> int *p = new int[100]();
The latter is ILLEGAL.
The former provides no initialization. Another major defect of the C++ language.
>
> My understanding is the first one doesn't and the second one does, just
> like for operator new.
Your understanding is again wrong.
Really? How would you explain that it works in Comeau? Another
"fucked-up Comeau extension"?
> The former provides no initialization. Another major defect of the C++
language.
> >
> > My understanding is the first one doesn't and the second one does, just
> > like for operator new.
>
> Your understanding is again wrong.
Is it? Read 5.3.4/15. I see (the fourth "bullet") "If the
new-initializer is in the form (), default initialization shall
be performed (8.5);" No mention of T being of class type, of
POD type, or of array thereof. Means, it applies to all of
them, no?
Please give your insight on that part of the Standard. Thanks.
Victor
You just love things like
struct IntGarbage { IntGarbage() {} int garbage; };
I guess. ;-)
regards,
alexander.
Just so there is no confusion, I labelled the above statements.
LINE BB and LINE CC are both valid, legal, and well-defined,
and usually mean different things. LINE AA is not allowed.
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
How can one verify that "as if"? There is no code to put
the breakpoint or a debugging output statement, is there?
That's what my question was about.
The whole idea for my "Really" was to coerce Ron into looking it
up in the Standard in an attempt to make him substantiate his
claims. It has become customary here (in response to some newbies'
"it works this way here so it's right" statements) to actually
quote the Standard (not that it has the desired effect every time),
so I would expect both Ron and you not to use "it's legal" or "it's
ILLEGAL" without proof. And, mind you, *I* don't need that proof,
most of the time. It's the other posters who read the forum I am
concerned about. :-)
Victor
Something about what causes it (in my opinion) :
http://groups.google.com/groups?selm=7a8veb%247u7%241%40nnrp1.dejanews.com
[snip]
==========================================
Alex Vinokur
mailto:ale...@connect.to
http://www.simtel.net/pub/oth/19088.html
http://sourceforge.net/users/alexvn
==========================================
>
> How can one verify that "as if"? There is no code to put
> the breakpoint or a debugging output statement, is there?
> That's what my question was about.
>
Debugging is not part of the language.
The constructor is treated AS IF it exists. Whether the compiler
emits code or calls it is an implementation detail. Read 12.1/5
and 12.1/7
>
> The whole idea for my "Really" was to coerce Ron into looking it
> up in the Standard in an attempt to make him substantiate his
> claims
Victor is right, Greg is right. I am wrong. The empty initializer parens
is permitted regardless of the type of new. Only ones with expressions
are prohiibited on arrays.