Is `new classname' the same as `new classname()' ?

Is `new classname' the same as `new classname()' ?

Post by Hongzheng » Fri, 05 Dec 2003 12:29:51


Hi,
I saw some codes use `new classname' to get a point to
a new object of classname, but some codes, instead, use
`new classname()'.
Is these two usage equivalent?

Thank you.

--
Hongzheng Wang
Department of Electronics Engineering
Tsinghua University
XXXX@XXXXX.COM
 
 
 

Is `new classname' the same as `new classname()' ?

Post by Victor Baz » Fri, 05 Dec 2003 13:01:10

"Hongzheng Wang" < XXXX@XXXXX.COM > wrote...

For POD they are not. The former leaves it uninitialised,
the latter makes it default-initialised, IIRC.

Victor

 
 
 

Is `new classname' the same as `new classname()' ?

Post by Xeno » Fri, 05 Dec 2003 23:39:07


No, both would cause default initialization. You cannot create an
uninitialized class object without a kludge.

DrX
 
 
 

Is `new classname' the same as `new classname()' ?

Post by Karl Heinz » Sat, 06 Dec 2003 00:01:13


They don't


you can:

#include <iostream>
using namespace std;

class A
{
public:
int m_n;
};

int main()
{
A* pA = new A;
A* pB = new A();


Here pA->m_n is left uninitialized while
pB->m_n should be default initialized.

VC++ 6.0 however gets this wrong and I suspect that
most compiler get this wrong. In other words: don't
depend on it.

Oh, by the way: Yes in the above, class A is a POD.

--
Karl Heinz Buchegger
XXXX@XXXXX.COM
 
 
 

Is `new classname' the same as `new classname()' ?

Post by Xeno » Sat, 06 Dec 2003 02:39:26


I don't care what you call it, both uses of the new operator will call the
default constructor.
 
 
 

Is `new classname' the same as `new classname()' ?

Post by Victor Baz » Sat, 06 Dec 2003 03:10:23

"Xenos" < XXXX@XXXXX.COM > wrote...



There is no default constructor for an int.

Now, are these two forms the same

int a;

and

int a = int();

? If not, what's the difference? What value does 'a' get in each
case?

And if you don't know that POD is (or what it stands for), perhaps
you need to learn first, then begin arguing...

Victor
 
 
 

Is `new classname' the same as `new classname()' ?

Post by Victor Baz » Sat, 06 Dec 2003 03:14:08

"Victor Bazarov" < XXXX@XXXXX.COM > wrote...


the


Oh, I meant to refer you to 5.3.4/15, but you wouldn't know where
to look, would you?
 
 
 

Is `new classname' the same as `new classname()' ?

Post by Xeno » Sat, 06 Dec 2003 06:57:13


the

If you refer back to my statement, I was talking about classes, not
primative types. The OP was asking about a struct, not an int.

And if you can't even read someone's statement, maybe you should learn
English before arguing.

DrX
 
 
 

Is `new classname' the same as `new classname()' ?

Post by Pete Becke » Sat, 06 Dec 2003 07:11:45


What Victor said is absolutely correct: the two forms are different for
a POD. And a struct or a class can be a POD.

--

Pete Becker
Dinkumware, Ltd. ( http://www.yqcomputer.com/ )
 
 
 

Is `new classname' the same as `new classname()' ?

Post by Xeno » Sat, 06 Dec 2003 07:22:20


call

I didn't say a struct couldn't be. But the different between the two forms
is largely syntactical. In reality, there is no difference in code
generation. If you want to think of them as meaning different things, fine.
It not worth arguing over.

DrX
 
 
 

Is `new classname' the same as `new classname()' ?

Post by Ron Natali » Sat, 06 Dec 2003 07:39:54


There's a substantial difference in code generation. It's not a "fine meaning."

class pod {
public:
int x;
};

new pod; // yields an uninitialized pod (x has indeterminate value).
new pod(); // yields a default initialized pod (x has value 0).
 
 
 

Is `new classname' the same as `new classname()' ?

Post by E. Robert » Sat, 06 Dec 2003 07:42:19


Yes.
 
 
 

Is `new classname' the same as `new classname()' ?

Post by Victor Baz » Sat, 06 Dec 2003 08:26:56

"E. Robert Tisdale" < XXXX@XXXXX.COM > wrote...


'nuther wun... Read 5.3.4/15 before continuing.
 
 
 

Is `new classname' the same as `new classname()' ?

Post by Pete Becke » Sat, 06 Dec 2003 09:12:22


There is also a difference in semantics. There is a difference in code
generation, because they mean different things. If you would stop being
arrogant long enough to read what everyone is saying you might learn
something.

--

Pete Becker
Dinkumware, Ltd. ( http://www.yqcomputer.com/ )
 
 
 

Is `new classname' the same as `new classname()' ?

Post by E. Robert » Sat, 06 Dec 2003 09:36:02


See the C++ FAQ [26.7] What is a "POD type"?
http://www.yqcomputer.com/ ++-faq-lite/intrinsic-types.html#faq-26.7


I don't think so.
I believe it is left up to the implementation
to determine whether the object is "initialized" or not.