Thread View: comp.std.c++
3 messages
3 total messages
Started by Daniel R. Edelson
Fri, 06 Nov 1992 22:13
Initialization of const dynamic objects
Author: Daniel R. Edelson
Date: Fri, 06 Nov 1992 22:13
Date: Fri, 06 Nov 1992 22:13
26 lines
1133 bytes
1133 bytes
Section 7.1.6 contains the following statement: ``Unless explicitly declared extern, a const object does not have external linkage and must be initialized.'' Does that prohibit the following code? new const int; This allocates an uninitialized const object. Seems like this should be explicitly prohibited (assuming there's no constructor, of course.) Is it? Incidently, section 5.3.3 says: ``If [a new-initializer is] not [present], the object will start out with an unspecified value.'' Thus, an unitialized const dynamic object has and keeps forever an unspecified value. This should be prohibited. None of the compilers I tested detected this error. Daniel Edelson
Re: Initialization of const dynamic objects
Author: Brian McCauley
Date: Sat, 07 Nov 1992 15:57
Date: Sat, 07 Nov 1992 15:57
24 lines
1330 bytes
1330 bytes
In article <1deqlgINNmc0@darkstar.UCSC.EDU>, daniel@cse.ucsc.edu (Daniel R. Edelson) writes:>
> new const int;
>
> This allocates an uninitialized const object.
> Seems like this should be explicitly prohibited (assuming
> there's no constructor, of course.) Is it?
> ...
> None of the compilers I tested detected this error.
Borland 3.1 does detect it. In fact I don't think it is necessary for a
compiler to spot such a mistake as the action of `new const int' is perfectly
well defined even if utterly useless!
BTW Borland rejects `new const A' (an implicit call to A::A()) but not
`new const A()' (an explicit call to A::A())
Does anyone know if this is the _standard_ behaviour?
\\ ( ) NO BULLSHIT! from BAM (Brian McCauley)
. _\\__[oo ============
.__/ \\ /\@
. l___\\ E-mail: B.A.McCauley@bham.ac.uk
# ll l\\
###LL LL\\
Re: Initialization of const dynamic objects
Author: John MAX Skaller
Date: Tue, 10 Nov 1992 01:28
Date: Tue, 10 Nov 1992 01:28
51 lines
1969 bytes
1969 bytes
In article <1deqlgINNmc0@darkstar.UCSC.EDU> daniel@cse.ucsc.edu (Daniel R. Edelson) writes:
>
>Section 7.1.6 contains the following statement:
>
> ``Unless explicitly declared extern, a const
> object does not have external linkage and
> must be initialized.''
>
>Does that prohibit the following code?
>
> new const int;
>
>This allocates an uninitialized const object.
IMHO there is no such thing as a const *object*. 'const' controls
the access to an object, const T and T are the same type (this not
how the ARM is written though).
However,
const int *p=new int;
makes p a pointer with const access to an undefined int. This is just like
int x; // no init
const int *p=&x;
>Seems like this should be explicitly prohibited (assuming
>there's no constructor, of course.) Is it?
>
>Incidently, section 5.3.3 says:
>
> ``If [a new-initializer is] not [present], the object
> will start out with an unspecified value.''
>
>Thus, an unitialized const dynamic object has and keeps
>forever an unspecified value. This should be prohibited.
Not necessarily. With p as above
*(int*)p=5; // p now points to 5, we can cast away const :-(
And of course
x=5;
--
;----------------------------------------------------------------------
JOHN (MAX) SKALLER, maxtal@extro.ucc.su.oz.au
Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads