SJohnson.ElSegundo@XEROX.COM (04/04/89)
Is the following code legal ? Since C/C++ doesn't allow lexical nesting of functions, I'd be inclined to believe that having My_Class::doit() inside func would be a no-no. As it turns out, Oregon Software's occ will compile and run the example below, while Gnu's g++ (version 1.32.0) goes into some kind of infinite loop during compilation. Swen Johnson SJohnson.ElSegundo@Xerox.COM ------------------------------------------------------------------------- #include <stream.h> void func() { class My_Class { int var; public: void doit() { var = 1; cout << "var = " << var << "\n"; }; }; My_Class my_object; my_object.doit(); }; main () { func(); }; -------------------------------------------------------------------------
hearn@claris.com (Bob Hearn) (04/04/89)
From article <890403-150348-4532@Xerox>, by SJohnson.ElSegundo@XEROX.COM: > Is the following code legal ? Since C/C++ doesn't allow lexical nesting of > functions, I'd be inclined to believe that having My_Class::doit() inside > func would be a no-no. > > As it turns out, Oregon Software's occ will compile and run the example > below, while Gnu's g++ (version 1.32.0) goes into some kind of infinite > loop during compilation. > > > Swen Johnson > SJohnson.ElSegundo@Xerox.COM > > ------------------------------------------------------------------------- > #include <stream.h> > > void func() { > class My_Class { [etc.] I don't know about classes nested in functions, but classes nested in classes are legal - except they don't mean what you think they do. Their scope is global. This is VERY ANNOYING. The one thing I hate most about C is lack of nested scoping. Bob Hearn hearn@claris.com
dan@oresoft.uu.net (Daniel Elbaum) (04/05/89)
In article <9418@claris.com> hearn@claris.com (Bob Hearn) writes:
:From article <890403-150348-4532@Xerox>, by SJohnson.ElSegundo@XEROX.COM:
:- Is the following code legal ?
:-
:- <declaration of function inside class inside function>
:
:I don't know about classes nested in functions, but classes nested in
:classes are legal - except they don't mean what you think they do. Their
:scope is global. This is VERY ANNOYING. The one thing I hate most about
:C is lack of nested scoping.
Declarations nested in class definitions are not global, but are limited
to the scope enclosing the class definition. Since class definitions are
normally performed outside of any block, nested declarations tend to take
on global scope.
A declaration of a class in C++ is similar to a declaration of a struture
in C. The template may be declared and used in any block, and the scope
of the template definition is the block within which it is defined. The
same is true for any data type. C certainly has nested scoping.
hearn@claris.com (Bob Hearn) (04/06/89)
From article <667@oresoft.uu.net>, by dan@oresoft.uu.net (Daniel Elbaum): > In article <9418@claris.com> hearn@claris.com (Bob Hearn) writes: > :From article <890403-150348-4532@Xerox>, by SJohnson.ElSegundo@XEROX.COM: > :- Is the following code legal ? > :- > :- <declaration of function inside class inside function> > : > :I don't know about classes nested in functions, but classes nested in > :classes are legal - except they don't mean what you think they do. Their > :scope is global. This is VERY ANNOYING. The one thing I hate most about > :C is lack of nested scoping. > > Declarations nested in class definitions are not global, but are limited > to the scope enclosing the class definition. Since class definitions are > normally performed outside of any block, nested declarations tend to take > on global scope. > > A declaration of a class in C++ is similar to a declaration of a struture > in C. The template may be declared and used in any block, and the scope > of the template definition is the block within which it is defined. The > same is true for any data type. C certainly has nested scoping. In a very limited sense. For C, the most important functionality of nested scoping is nested functions, which you can't have. In C++, you also want nested classes, which you can't have. These are major deficiencies. With C++, you can hack around the nested function problem by declaring a class within your function that has member functions which are effectively nested functions. But why should you have to? Bob Hearn hearn@claris.com
danw@tekchips.LABS.TEK.COM (Daniel E. Wilson) (04/07/89)
In article <667@oresoft.uu.net>, dan@oresoft.uu.net (Daniel Elbaum) writes: > A declaration of a class in C++ is similar to a declaration of a struture > in C. The template may be declared and used in any block, and the scope > of the template definition is the block within which it is defined. The > same is true for any data type. C certainly has nested scoping. 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. Dan Wilson
ark@alice.UUCP (Andrew Koenig) (04/08/89)
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