This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Class casting
Justin Miller <millerj@wam.umd.edu> writes:
> Interestingly enough, if you did this:
>
> int main() {
> B b;
> C* c = new C(b);
This alone is not enough. The OP used private inheritance, and will
therefor get an error like this:
cl.cc: In function `int main()':
cl.cc:26: error: `A' is an inaccessible base of `B'
> }
>
> It will compile and work as expected. The constructor wants an A, a B is
> an A, so everything is fine.
>
> Why one needs to declare the B first, rather than instantiating the
> temporary as in the code below, I have no idea. Can anyone shed some
> light on this?
[snip]
The C constructor in the OP's code was C::C(A& ) ; it took a reference
to non-const. It is ill-formed to bind a temporary to a reference
to non-const.