DMD OSX / Segfault 11
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Feb 2 11:03:04 PST 2016
On 02.02.2016 19:06, Robert M. Münch wrote:
> I have a very strange effect, I'm not sure what it is about:
>
> Value: {}
>
> WordV: Value {
> Value* get() {}
> }
>
> BaseOperator: Value : {
> }
This isn't valid D code at all, which makes it unnecessarily hard to
understand what you mean.
> Now comes the code using this:
>
> auto op = cast(BaseOperator)(Word.get());
> if (op !is null) {...
>
>
> If get() returns "Value*" it segfaults, if I change it to "Value" it
> works. How can this be?
A Value* is a pointer to a class reference. Unless you're doing
something really funky with the pointer, casting it to a class type
doesn't make sense.
Casting between class types that have an inheritance relation, like
Value and BaseOperator, does make sense (upcat/downcast).
If anything, you should be casting between Value* and BaseOperator*
(both pointer types) if you want to do something with pointers.
But you very seldom need pointers to class references. Just return Value
from get, and cast to BaseOperator.
More information about the Digitalmars-d-learn
mailing list