Xref: utzoo comp.lang.c++:11524 comp.lang.c:35920 comp.std.c++:580 comp.std.c:4270 Path: utzoo!mnetor!tmsoft!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!helios!bcm!dimacs.rutgers.edu!seismo!uunet!brunix!sdm From: sdm@cs.brown.edu (Scott Meyers) Newsgroups: comp.lang.c++,comp.lang.c,comp.std.c++,comp.std.c Subject: Are addresses of const members const? Message-ID: <63928@brunix.UUCP> Date: 6 Feb 91 20:27:19 GMT Sender: news@brunix.UUCP Reply-To: sdm@cs.brown.edu (Scott Meyers) Organization: Brown University Department of Computer Science Lines: 35 For both ANSI C and as-ANSI-as-we-have C++, are the members of a const struct themselves const? Consider this: 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? If so, there should be a type mismatch between the expression being returned (const char *) and the function's return type (char *). 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. The same question can be put this way: is the following legal? const struct foo x; char **p = &(x.data); /* lhs = char **, rhs = const char ** */ If the members of a const structure are themselves supposed to be const, I'd appreciate it if you'd point me to an authoritative reference that says so. For C++, I looked in the ARM, but couldn't find anyplace where it said what should happen. For C, all I've got is the second (ANSI C-based) edition of K&R, but I couldn't find any information in there, either. Thanks, Scott ------------------------------------------------------------------------------- What do you say to a convicted felon in Providence? "Hello, Mr. Mayor."