This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
g++ 3.2 and dynamic_cast for private base
- From: "Moore, Mathew L" <MooreML at BATTELLE dot ORG>
- To: "'gcc-help at gcc dot gnu dot org'" <gcc-help at gcc dot gnu dot org>
- Date: Tue, 17 Sep 2002 16:14:40 -0400
- Subject: g++ 3.2 and dynamic_cast for private base
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;
}