By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,346 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

new operator detail?

P: n/a
classname *ptr = new classname();

Any idea why the parenthesis in the end?I thought that " new " worked just
fine with the classname alone?
Thanks in advance.
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Giorgos P." <gp******@ee.duth.gr> wrote...
classname *ptr = new classname();

Any idea why the parenthesis in the end?I thought that " new " worked just
fine with the classname alone?


If 'classname' is a POD (plain old data), the difference is
that without parentheses the object is left uninitialised,
and with parentheses it is default-initialised. For non-POD
types it doesn't matter.

Victor
Jul 19 '05 #2

P: n/a
Giorgos P. wrote:
classname *ptr = new classname();

Any idea why the parenthesis in the end?I thought that " new " worked just
fine with the classname alone?


I believe that if you use the classname without parenthesis, it just
calls the default, no-argument constructor. In other words, they are
equivalent.

The parentheses syntax is required if you have a constructor that takes
arguments.

- Adam

Jul 19 '05 #3

P: n/a
Victor Bazarov wrote:
<snip>
If 'classname' is a POD (plain old data), the difference is
that without parentheses the object is left uninitialised,
and with parentheses it is default-initialised. For non-POD
types it doesn't matter.

Ah, right.

Ignore my other post in this thread. I was wrong.

- Adam

Jul 19 '05 #4

P: n/a
Giorgos P. wrote:
classname *ptr = new classname();

Any idea why the parenthesis in the end?I thought that " new " worked just
fine with the classname alone?
...


It depends on the type designated by 'classname'. Unfortunately, you
didn't provide any details about this type.

If 'classname' names a non-POD class type, the '()' makes no difference
whatsoever, i.e. 'new classname' and 'new classname()' do the same thing.

For POD types the '()' is essential - it causes the object to be
default-initialized, while 'new classname' will not initialize it at all.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.