Path: utzoo!attcan!uunet!mcsun!ukc!dcl-cs!aber-cs!pcg From: pcg@aber-cs.UUCP (Piercarlo Grandi) Newsgroups: comp.lang.c++ Subject: Re: Scoping bug??? Summary: A local scope class is really global... Message-ID: <1729@aber-cs.UUCP> Date: 13 Apr 90 15:03:24 GMT Reply-To: pcg@cs.aber.ac.uk (Piercarlo Grandi) Organization: Dept of CS, UCW Aberystwyth (Disclaimer: my statements are purely personal) Lines: 37 In article <2582@ektools.UUCP> randolph@ektools.UUCP (Gary L. Randolph) writes: Am I missing somthing? Sure smells like a bug. Reading TFM... :-) Your example, rewritten in a terser way, goes like this: f() { class X { ... }; X localX; ... } X globalX; In C++ it has always been true that the scope of a class name is always global, modulo redeclarations in inner scopes. In C++ 2.0 the issue of local scope class definitions has been clarified, in particular with reference to the problem of member functions defined within the local class definition. Essentially such class declarations are as though defined in the global scope, save for some restrictions. Consider the following fragment and try it on your favourite compiler for some minutes of innocent merryment: void f() { struct S { float f; unsigned m() { return this->f == 1.0;} }; } struct S s1 = 1.0; void g() { struct S { char c; unsigned m() { return this->c == '@'} }; } struct S s2 = '@'; extern "C" extern printf(const char *,...); main() { printf("%u %u\n",s1.m(),s2.m()); return 0; } Try adding 'struct S;' at the beginning of 'f()' or 'g()', or another definition of 'struct S' before 'f()'. Unless you have very clear reasons to use them, stay clear of classes defined in a block scope. -- Piercarlo "Peter" Grandi | ARPA: pcg%cs.aber.ac.uk@nsfnet-relay.ac.uk Dept of CS, UCW Aberystwyth | UUCP: ...!mcvax!ukc!aber-cs!pcg Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk