Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!pasteur!ucbvax!tut.cis.ohio-state.edu!rutgers!att!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: Are Class Decl.'s Legal in Funcs ? Message-ID: <9162@alice.UUCP> Date: 7 Apr 89 19:09:16 GMT References: <890403-150348-4532@Xerox> <9418@claris.com> <667@oresoft.uu.net> <3817@tekcrl.LABS.TEK.COM> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 61 In article <3817@tekcrl.LABS.TEK.COM>, danw@tekchips.LABS.TEK.COM (Daniel E. Wilson) writes: > A structure or class nested in a sturcture or a class will have a global > scope in C++. Yes, this is very annoying and unexpected since it differs > from what C does. Unless the language has been changed recently. No, it *is* what C does, almost. C says that nested structure definitions are flattened out: struct A { /* stuff */ struct B { /* other stuff */ }; }; is equivalent to struct B { /* other stuff */ }; struct A { /* stuff */ }; (more or less), so that's what C++ does. On the other hand, structure definitions nested inside functions are local. C++ 1.2 has various bugs associated with nesting class definitions inside functions. These will be fixed in 2.0. However, C++ still has to live with the limitation that C doesn't permit nested functions definitions. This means that member functions of classes nested inside function definitions cannot access local variables of those functions. Example: int a; f() { int a; struct X { void zap() { a = 0; // which a? } }; X x; x.zap(); } The answer to `which a?' is, unfortunatly, `the global one'. Otherwise there's no sensible way to compile this into C. Yes, it could be done in this case by expanding zap() inline. But inline expansion should be transparent, and what if zap() were a thousand lines instead of one? -- --Andrew Koenig ark@europa.att.com