[comp.lang.c++] Scoping bug???

randolph@ektools.UUCP (Gary L. Randolph) (04/11/90)

This was posted twice previously but I have reason to believe it was
only sent locally (didn't get outside Rochester). 
 
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.

By commenting out the member function (and the call) the compiler
correctly generates an error, but as is, the compiler does not
recognize a problem.  Am I missing somthing? Sure smells like a bug.


#include <iostream.h>
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: "<<c.i<<"\nf: "<<c.f<<endl;
func();
}

void func(){
X x;         //COMPILER TAKES THIS
x.i = 2;
x.f = 44.5;
cout<<"\ni: "<<x.i<<"\nf: "<<x.f<<endl;
}


Thanks, 
      Gary

zhu@crabcake.cs.jhu.edu (Benjamin Zhu) (04/12/90)

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.
>
>By commenting out the member function (and the call) the compiler
>correctly generates an error, but as is, the compiler does not
>recognize a problem.  Am I missing somthing? Sure smells like a bug.
>
>
>#include <iostream.h>
>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: "<<c.i<<"\nf: "<<c.f<<endl;
>func();
>}
>
>void func(){
>X x;         //COMPILER TAKES THIS
>x.i = 2;
>x.f = 44.5;
>cout<<"\ni: "<<x.i<<"\nf: "<<x.f<<endl;
>}
>
>
>Thanks, 
>      Gary

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).

	A class can be defined within the scope of a file. Its class name
	is visible only within the boundary of its local scope. blah, blah,
	blah...

I do not know why we have this bug here. Does AT&T give different versions
of the cfront translator?!

Benjamin Zhu
=======================================================================
Sorry, but I have no disclaimer for you.
=======================================================================

pcg@aber-cs.UUCP (Piercarlo Grandi) (04/13/90)

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

pcg@aber-cs.UUCP (Piercarlo Grandi) (04/13/90)

In article <1729@aber-cs.UUCP> pcg@cs.aber.ac.uk (Piercarlo Grandi) writes:
  
  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.

I realized that I have not been explicit about splitting hairs finely
enough; all this is somewhat different across the cases of class local to a
class, to a function, whether you are talking 1.2 or 2.0 or later (hints of
revisions), and whether you use G++ or cfront or some other compiler.
-- 
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