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]

[Bug c++/21693] New: private member is accessible by a derived class when declaring and defining classes in the same file


In the following code, the private member `n' of class `C' can be accessed by
derived class `D'.
The fact that g++ does not complain depends on the order of the declarations.
Typically, moving the `C::n' initialization elsewhere in the code will trigger
(as expected) the following error:

$ error: `const int C::n' is private

//----------------------------------------------------
// private.cpp

#include <iostream>

class C {
private:
   static const int n;
};

const int C::n = 9;

class D : public C {
public:
   void print_n() { std::cout << "n=" << n << "\n"; }
} d;

int main()
{
   d.print_n();
   return 0;
}

//----------------------------------------------------

Additional information:

    * the exact version of GCC;
    * the system type;
    * the options given when GCC was configured/built;

$ g++ -v
Reading specs from /usr/lib/gcc-lib/i586-mandrake-linux-gnu/3.3.2/specs
Configured with: ../configure --prefix=/usr --libdir=/usr/lib
--with-slibdir=/lib --mandir=/usr/share/man --infodir=/usr/share/info
--enable-shared --enabl
e-threads=posix --disable-checking --enable-long-long --enable-__cxa_atexit
--enable-clocale=gnu --enable-languages=c,c++,ada,f77,objc,java,pascal --host=i
586-mandrake-linux-gnu --with-system-zlib
Thread model: posix
gcc version 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)

    * the complete command line that triggers the bug;

g++ -Wall -Werror -ansi -pedantic -o private private.cpp && ./private

    * the compiler output (error messages, warnings, etc.); and

none! It should complain that `const int C::n' is private

-- 
           Summary: private member is accessible by a derived class when
                    declaring and defining classes in the same file
           Product: gcc
           Version: 3.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sylvain dot guilley at enst dot fr
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21693


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