c++/10016: g++ allows access to static private class members if initialized

thor@math.tu-berlin.de thor@math.tu-berlin.de
Mon Mar 10 15:03:00 GMT 2003


>Number:         10016
>Category:       c++
>Synopsis:       g++ allows access to static private class members if initialized
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          accepts-illegal
>Submitter-Id:   net
>Arrival-Date:   Mon Mar 10 14:26:01 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Thomas Richter
>Release:        g++ (GCC) 3.2
>Organization:
>Environment:
Linux i386
>Description:
g++ accepts the following code without generating a warning or an error, though it should warn about trying to access the private member "foo" of class A:

/* snip */

class A {
  static int foo;
};
int A::foo = 42;

class B : public A {
public:
  int MyFoo(void) 
  { 
    return foo; 
  }
};

int main(int argc,char **argv)
{
  class B b;

  return b.MyFoo();
}
>How-To-Repeat:
Save the above program as "foo.cpp", then compile as follows:

$ g++ -Wall foo.cpp

g++ will compile the program without generating an error or a warning, even though A::foo is private within B::MyFoo. g++ will warn if the initializer for A::foo is moved out of sight.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list