public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* g++ 3.2 and dynamic_cast for private base
@ 2002-09-17 13:16 Moore, Mathew L
  0 siblings, 0 replies; only message in thread
From: Moore, Mathew L @ 2002-09-17 13:16 UTC (permalink / raw)
  To: 'gcc-help@gcc.gnu.org'

I recently downloaded and installed gcc 3.2, and now I find that I cannot
use the dynamic_cast operator for casting `this' to a private base class
type.  I know this latest release conforms more to the ISO standard, but I
can't find anything in the standard that forbids this operation.

I would appreciate if someone could tell me where I went awry!

Thanks in advance for your help.

--Matt


The following code gives me the errors:

test.cpp: In member function `int C::methodC() const':
test.cpp:28: `A' is an inaccessible base of `C'
test.cpp:29: `A' is an inaccessible base of `C'

(lines are marked by comments in the code).

Built with `g++ --ansi -Wall '

====================
#include <iostream>

class A 
{
public:
  int methodA() const {
    return 1;
  }
};

class B : public A 
{
public:
  int methodB() const {
    const A& a = dynamic_cast<const A&>(*this);
    return a.methodA() + 1;
  }
};

class C : private B
{
public:
  int methodC() const {
    // This works:
    const A& implicit_cast = *this;
    // But the next two source lines generate "inaccessable base"
    // errors.
    const A* pa = dynamic_cast<const A*>(this);
    const A& a = dynamic_cast<const A&>(*this);
    return implicit_cast.methodA() + 2;
  }
};

int
main(int argc, char** argv)
{
  C c;
  std::cout << "result = " << c.methodC();
  return 0;
}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-09-17 20:16 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-17 13:16 g++ 3.2 and dynamic_cast for private base Moore, Mathew L

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).