[Bug c++/10990] New: get error when I use dynamic_cast on protected base class

rds20@attbi.com gcc-bugzilla@gcc.gnu.org
Tue May 27 04:56:00 GMT 2003


PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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

           Summary: get error when I use dynamic_cast on protected base
                    class
           Product: gcc
           Version: 3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: rds20@attbi.com
                CC: gcc-bugs@gcc.gnu.org

Hello,

Please look into this bug and correct it soon.

Severity: serious: GCC is not working properly; a work-around is possible.
Category: compliance with C++ standard.
Class: rejects-legal: GCC gives an error message for correct code.
Environnment: using MinGW 4.9.8.0 on Win-XP.

Description:
The Junk::AsBase() function shown below should be able to perform a dynamic_cast
on itself and return a pointer to JunkBase.  However, the compiler says the base
class is inaccessible even though class Junk should be able to call any
functions in its base class and also return a pointer to itself as the base class.

Fix:
I can work around this by declaring JunkBase as a public base class.  But that
allows functions provided by JunkBase to be called by any other function, and I
don't want to make those functions accessible to anybody.


This is the Junk.ii file created by the compiler.

# 1 "junk.cpp"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "junk.cpp"

class JunkBase
{
public:
    virtual void DoSomething( void ) = 0;
protected:
    virtual ~JunkBase( void ) {};
    JunkBase( void ) {}
};

class Junk : protected JunkBase
{
public:
    Junk( void ) : JunkBase() {}
    virtual ~Junk( void ) {}
protected:
    inline JunkBase * AsBase( void )
    { return dynamic_cast< JunkBase * >( this ); }
    virtual void DoSomething( void ) { }
};




This is the command line I used to expose the bug:
g++.exe -c junk.cpp -o junk.o -I"C:/Dev-Cpp/include/c++" 
-I"C:/Dev-Cpp/include/c++/mingw32"  -I"C:/
Dev-Cpp/include/c++/backward"  -I"C:/Dev-Cpp/include"  -I"C:/Projects/Include" 
-save-temps
junk.cpp: In member function `JunkBase* Junk::AsBase()':
junk.cpp:16: `JunkBase' is an inaccessible base of `Junk'
c:\Dev-Cpp\bin\make.exe: *** [junk.o] Error 1




g++ version info:
Reading specs from c:/Dev-Cpp/bin/../lib/gcc-lib/mingw32/3.2/specs
Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as
--host=mingw32 --target=min
gw32 --prefix=/mingw --enable-threads --disable-nls
--enable-languages=f77,c++,objc,ada --disable-wi
n32-registry --disable-shared
Thread model: win32
gcc version 3.2 (mingw special 20020817-1)



Thanks for your attention to this.

Rich



------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.



More information about the Gcc-bugs mailing list