Xref: utzoo comp.lang.c++:11517 comp.lang.c:35913 comp.std.c++:579 comp.std.c:4264 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!olivea!uunet!spool.mu.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!dptg!ulysses!andante!alice!ark From: ark@alice.att.com (Andrew Koenig) Newsgroups: comp.lang.c++,comp.lang.c,comp.std.c++,comp.std.c Subject: Re: Are addresses of const members const? Message-ID: <11849@alice.att.com> Date: 7 Feb 91 19:20:27 GMT References: <63928@brunix.UUCP> Reply-To: ark@alice.UUCP () Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 36 In article <63928@brunix.UUCP> sdm@cs.brown.edu (Scott Meyers) writes: > struct Foo { > char *data; > }; > char * f(const struct Foo x) > { > return x.data; > } > Within function f, x is a const. Is x.data therefore a const? Yes, in both ANSI C and C++, but that doesn't have the implications you think it does. For example, the ANSI C standard gives the following example in section 3.5.3 (slightly abbreviated here): const struct s { int mem; } cs = { 1 }; int * pi; pi = &cs.mem; /* violates type constraints for = */ > Yet this example > sails through 3 C++ compilers (g++, cfront 2.0, Sun cfront 2.1 beta) and > one ANSI C compiler (gcc) without so much as a wimper. When you say that x is a const Foo, that means that x.data is itself a constant -- but that doesn't say anything about the memory to which x.data points. In other words, the type of x.data is char *const, which is distinct from const char *. In particular, there is no problem assigning a char *const to a char *. -- --Andrew Koenig ark@europa.att.com