Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is `new classname' the same as `new classname()' ?

Reply
Thread Tools

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

 
 
Hongzheng Wang
Guest
Posts: n/a
 
      12-04-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-04-2003
"Hongzheng Wang" <(E-Mail Removed)> wrote...
> 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?


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

Victor


 
Reply With Quote
 
 
 
 
Xenos
Guest
Posts: n/a
 
      12-04-2003

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:akyzb.416658$Fm2.425058@attbi_s04...
> "Hongzheng Wang" <(E-Mail Removed)> wrote...
> > 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?

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

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

DrX


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      12-04-2003
Xenos wrote:
>
> "Victor Bazarov" <(E-Mail Removed)> wrote in message
> news:akyzb.416658$Fm2.425058@attbi_s04...
> > "Hongzheng Wang" <(E-Mail Removed)> wrote...
> > > 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?

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

> No, both would cause default initialization.


They don't

> You cannot create an
> uninitialized class object without a kludge.
>


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
(E-Mail Removed)
 
Reply With Quote
 
Xenos
Guest
Posts: n/a
 
      12-04-2003

"Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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.
>

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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-04-2003
"Xenos" <(E-Mail Removed)> wrote...
>
> "Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > 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.
> >

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


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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-04-2003
"Victor Bazarov" <(E-Mail Removed)> wrote...
> "Xenos" <(E-Mail Removed)> wrote...
> >
> > "Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >
> > > 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.
> > >

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

the
> > default constructor.



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

> 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...




 
Reply With Quote
 
Xenos
Guest
Posts: n/a
 
      12-04-2003

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:jMKzb.226707$Dw6.803084@attbi_s02...
> "Xenos" <(E-Mail Removed)> wrote...
> >
> > "Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >
> > > 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.
> > >

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

the
> > default constructor.

>
> 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
>
>


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


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      12-04-2003
Xenos wrote:
>
> "Victor Bazarov" <(E-Mail Removed)> wrote in message
> news:jMKzb.226707$Dw6.803084@attbi_s02...
> > "Xenos" <(E-Mail Removed)> wrote...
> > >
> > > "Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > >
> > > > 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.
> > > >
> > > I don't care what you call it, both uses of the new operator will call

> the
> > > default constructor.

> >
> > 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
> >
> >

>
> 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.
>


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.dinkumware.com)
 
Reply With Quote
 
Xenos
Guest
Posts: n/a
 
      12-04-2003

"Pete Becker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Xenos wrote:
> >
> > "Victor Bazarov" <(E-Mail Removed)> wrote in message
> > news:jMKzb.226707$Dw6.803084@attbi_s02...
> > > "Xenos" <(E-Mail Removed)> wrote...
> > > >
> > > > "Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
> > > > news:(E-Mail Removed)...
> > > >
> > > > > 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.
> > > > >
> > > > I don't care what you call it, both uses of the new operator will

call
> > the
> > > > default constructor.
> > >
> > > 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
> > >
> > >

> >
> > 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.
> >

>
> 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.dinkumware.com)


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prevent two users from accessing the same file at the same time Shawn ASP .Net 2 02-19-2006 03:11 AM
how to use same user control x number of times on same asp page David Hubbard ASP .Net 2 01-12-2006 03:56 PM
Local LAN Access - Works for WinXP, No Go For Linux - Same Server,Same Laptop Marc Schwartz Cisco 0 06-18-2005 02:28 PM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Two PIX on same subnet with same gateway? This Old Man Cisco 4 10-20-2003 07:27 PM



Advertisments