This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C++ class members do not always default to `private'


These two classes should behave the same:

  class { friend class A; int x; };
  class { friend class A; private: int x; };

Below is an example where they behave differently in g++ 3.2, but not in
g++ 2.95.3.

g++ 3.2 gives a compile error only for an access to the second class,
while g++ 2.95.3 gives a compile error for access to the 'x' member of
both classes, as expected.

Self-explanatory shell transcript follows:


(martin@wobble) ~/src $ cat class_private_bug.cc
class A
{
public:
  class { friend class A; int x; } m1;
  class { friend class A; private: int x; } m2;
} a;

int
main (int argc, char *argv[])
{
  a.m1.x;
  a.m2.x;
  return 0;
}
(martin@wobble) ~/src $ g++ class_private_bug.cc
class_private_bug.cc: In function `int main(int, char**)':
class_private_bug.cc:5: `int A::<anonymous struct>::x' is private
class_private_bug.cc:12: within this context
zsh: exit 1     g++ class_private_bug.cc
(martin@wobble) ~/src $ /usr/bin/g++ class_private_bug.cc
class_private_bug.cc: In function `int main(int, char **)':
class_private_bug.cc:4: `int A::{anonymous class}::x' is private
class_private_bug.cc:11: within this context
class_private_bug.cc:5: `int A::{anonymous class}::x' is private
class_private_bug.cc:12: within this context
zsh: exit 1     /usr/bin/g++ class_private_bug.cc
(martin@wobble) ~/src $  g++ --version; /usr/bin/g++ --version; uname -a
g++ (GCC) 3.2
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

2.95.3
Linux wobble 2.4.10-4GB #1 Fri Sep 28 17:20:21 GMT 2001 i586 unknown


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]