No implicitly convert derived ptr to base ptr?

Jonathan M Davis jmdavisProg at gmx.com
Tue Apr 24 23:02:38 PDT 2012


On Wednesday, April 25, 2012 01:50:51 Nick Sabalausky wrote:
> The compiler rejects this:
> 
>     class Base {}
>     class Derived : Base {}
> 
>     void main()
>     {
>         Base*    basePtr;
>         Derived* derivedPtr;
> 
>         basePtr = derivedPtr; // ERROR
>     }
> 
> Is that really correct that it shouldn't be allowed?

Well, given that pointers aren't polymorphic at all, it's not all that great 
an idea in general to assign a derived class object to a base class pointer, 
so it's arguably a good thing that it doesn't work, but I am a bit surprised 
that it doesn't work.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list