Topic: new int operator
Author: James.Kanze@dresdner-bank.com
Date: 2000/11/30 Raw View
In article <3A2404BA.36E01662@sensor.com>,
Ron Natalie <ron@sensor.com> wrote:
> For classes, the two forms (with and without the empty arg list) are
> equivelent. The default constructor is run in either case.
Only for non-POD classes. POD classes and unions are zero
initialized, exactly like other POD types.
--
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: famharvey@aol.com (Famharvey)
Date: 2000/11/28 Raw View
Hi,
New to this NG, sorry if it the wrong forum.
I was playing with some code and wondered on the following:
int *i = new int;
int *j = new int();
int *k = new int(10);
Of those examples, what should I expect from standard C++?
Will the middle statement assign a default value (0) to *j, or is that my
compiler making the decision (C++ Builder)?
Is the last statement defined in standard C++?
Do the same rules apply to all built-in types (that a default value is created
in the second statement, and that a "copy constructor" initialization can be
used with new)?
Thanks,
Rob
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Andrei Iltchenko <Andrei.Iltchenko@openmarket.com>
Date: 2000/11/28 Raw View
Famharvey wrote:
> Hi,
> New to this NG, sorry if it the wrong forum.
>
> I was playing with some code and wondered on the following:
>
> int *i = new int;
>
> int *j = new int();
>
> int *k = new int(10);
>
> Of those examples, what should I expect from standard C++?
> Will the middle statement assign a default value (0) to *j, or is that my
> compiler making the decision (C++ Builder)?
> Is the last statement defined in standard C++?
>
> Do the same rules apply to all built-in types (that a default value is created
> in the second statement, and that a "copy constructor" initialization can be
> used with new)?
>
> Thanks,
> Rob
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
> [ Note that the FAQ URL has changed! Please update your bookmarks. ]
All the three examples are well-formed according to standard C++.
For the first statement, the object pointed to by i will have an
indeterminate value, as the type constitutes a POD type.
For the second, the object pointed to by j will be default-initialized,
which in the case of POD types means zero-initialization.
As for the third statement, the object pointed to by k will be initialized
to 10, because the new-initializer contains exactly one expression and
the new-type-id constitutes an arithmetic type. See text in 5.3.4 paragraph
15 of the Standard for more details.
Regards,
Andrei.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/11/29 Raw View
Famharvey wrote:
The standard behavior is:
> int *i = new int;
Allocates a single int, uninitialized.
> int *j = new int();
Allocates a single int, default (zero) initializes it.
to it.
>
> int *k = new int(10);
Allocates a single int, intializes it to 10.
> Do the same rules apply to all built-in types (that a default value is created
> in the second statement,
Yes. For the builtin types saying "new TYPE" or allocating a automatic
variable of the TYPE leaves it uninitialized. The empty arg list in new
causes it to be default (zero) initialized.
For classes, the two forms (with and without the empty arg list) are
equivelent. The default constructor is run in either case.
> that a "copy constructor" initialization can be used with new)?
>
A copy constructor can be used with new, sure why not? Other than the
fact that one will be implicitly generated for you, the copy construction
is no different that any other constructor form.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]