Structs with pointers?

Sean Eskapp eatingstaples at gmail.com
Sat Jan 22 15:00:52 PST 2011


== Quote from bearophile (bearophileHUGS at lycos.com)'s article
> Sean Eskapp:
> > Nevermind, I realized it's because constness is transitive in pointers. A const
> > struct with a pointer member has a const pointer member, and those can't be
> > implicitly cast to non-const pointer members.
> Uhm, the struct having a pointer is not important. In theory this too can't compile:
> struct Bar {}
> void foo(Bar b) {}
> void main() {
>     const b = Bar();
>     foo(b);
> }
> Is this another compiler bug?
> Bye,
> bearophile

No, that compiles fine. Because the struct owns a pointer, the const struct owns a
const pointer. When copying a const-struct to a non-const struct, this means you
must cast a const-pointer to a non-const pointer, which shouldn't be allowed.


More information about the Digitalmars-d-learn mailing list