Robert Greig escribió:
2008/6/3 Alan Conway <[EMAIL PROTECTED]>:
gcc and solaris are differing on the rules about casting to private bases.
I'm not certain if it's tecnically incorrect but a bit of googling strongly
suggests that we should not write code that depends on this, we will
probably have trouble with other compilers.
I am pretty sure that it is not valid to attempt to cast to a private
base class (which makes sense since the relationship is supposed to
be, well, private). It is a while since I have done any C++ but I seem
to recall that Microsoft tightened up VS 2005 so that it rejected
this.
RG
Well, I'm not sure if the problem is simply forbidding to cast to a
private base class. Doing that directly fails in compile-time, in both
Sun CC and g++. The problem arises when the private class is passing
itself (as *this) to another function accepting a superclass of that
private class. Trying to dynamic_cast to the private class fails in Sun
CC and works in g++. However, casting to a private base class fails in
compile time on both compilers:
cat test.cpp
struct A { virtual ~A() {}};
struct B : private A{} b;
A* pa = dynamic_cast<A*>(&b);
CC -c test.cpp
"test.cpp", line 3: Error: Cannot cast from B* to A*.
g++ -c test.cpp
test.cpp:3: error: 'A' is an inaccessible base of 'B'
Best regards.
--
Manuel.