Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uflorida!haven!umd5!crabcake!zhu From: zhu@crabcake.cs.jhu.edu (Benjamin Zhu) Newsgroups: comp.lang.c++ Subject: Re: Scoping Bug Message-ID: <1283@crabcake> Date: 12 Apr 90 18:04:11 GMT References: <36159@brunix.UUCP> Reply-To: zhu@crabcake.cs.jhu.edu (Benjamin Zhu) Followup-To: comp.lang.c++ Organization: Johns Hopkins University CS Dept. Lines: 68 In article <36159@brunix.UUCP> sdm@cs.brown.edu (Scott Meyers) writes: >In article <1251@crabcake> zhu@crabcake.cs.jhu.edu (Benjamin Zhu) writes: >>In article <2582@ektools.UUCP> randolph@ektools.UUCP (Gary L. Randolph) writes: >>[stuffs deleted here] >>>There appears to be a scoping problem with locally defined classes >>>that contain a member function. The following example compiles without >>>a hitch on my Sun. I am using Sun's 2.0 cfront. >>> >>>#include >>>main() >>>{ >>>void func(); //prototype >>> >>>//local class definition >>>class X{ >>> public: >>> int i; >>> float f; >>> void printIt(){cout<<"\nHello\n";} >>>}c; >>>c.i = 1; >>>c.f = 3.3; >>>c.printIt(); >>>cout<<"\ni: "<>>func(); >>>} >>> >>>void func(){ >>>X x; //COMPILER TAKES THIS >>>x.i = 2; >>>x.f = 44.5; >>>cout<<"\ni: "<>>} >> >>Our AT&T cfront 2.0 also displays the same problem. Personally I believe >>it is a scoping bug. Actually there is a paragraph concerning this in >>Lippman's C++ Primer (pp222-223). > >This is not a bug, it is the way C++ is defined to behave. Basically, >there is no such thing as a "locally defined class" in C++. Classes >defined inside classes and classes defined inside functions are all in the >global name space. > >Scott Not really. Suppose the local class behaves as you claimed, then the member functions, like X::printIt() here, should be allowed to be defined outside the body of function its class is defined in (say main() here), which is not the case when you program in cfront 2.0. Check it out and you will know the fact. Local defined classes do exist! Personally, I believe the local class in C++ is a kind of tradeoff of limited levels of lexical scoping (at most two levels) and consistency with C programming. However, it's really premature at this stage and is generally not usefull. Here is a quote from Lippman: A local class makes sense when its definition is simple and its use is limited to the function within which it is defined. However, given that C and C++ have at most two levels of scoping (file scope and function scope), I would not expect much can be done soon. Benjamin Zhu ================================================================= Sorry, but I have no disclaimer for you. =================================================================