Re: dynamic_cast from a const class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> The dynamic_cast operator shall not cast away constness (5.2.11).
>
>
> class A {
> };
>
> class B : public A {
> };
>
> int main() {
>    A* a;
>    B* b = new B();
>    a = dynamic_cast<const A*>(b);
>    return 0;
> }
>

You aren't casting *away* const-ness, you are adding const-ness (and then
discarding it when you assign to A - which should be an error).

Now if you had done:

int main() {
    A* a;
    const B* b = new B();
    a = dynamic_cast<A*>(b);
    return 0;
 }

Then you'd be casting away const-ness.







[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux