Re: New C++ warning about comparing this==NULL
Re: New C++ warning about comparing this==NULL
- Subject: Re: New C++ warning about comparing this==NULL
- From: Doug Hill <email@hidden>
- Date: Thu, 03 Dec 2015 11:08:17 -0800
>
>> On Dec 2, 2015, at 10:41 PM, Clark Cox <email@hidden> wrote:
>>
>>> Widget *w = factory.getCurrentWidget();
>>> Paint *p = w->getPaint();
>>> If the currentWidget is NULL, the second line does this.
>>
>> That counts as a pointer dereference (the special case of &*foo is just
>> that, a special case).
>
> It syntactically looks like one, but it isnât one. âDereferenceâ
> means referencing, i.e. accessing, the memory location pointed to. A
> nonvirtual method call doesnât do that.
>
> The reason itâs undefined in the standard to dereference NULL is because
> on almost all operating systems thatâs unmapped memory and will cause a
> crash. It isnât just because of squeamishness about what the LHS of a ->
> operator should be.
>
>> On Dec 2, 2015, at 10:22 PM, Marco S Hyman <email@hidden> wrote:
>>
>> Isnât it a compiler implementation detail that w->getPaint under the
>> covers turns into getPaint(w)?
>
>
> In many languages Iâd agree with you. But C++ has a very explicit design
> principle of not adding extra overhead unless you ask for it. If you want
> the more powerful (but slower) behavior of virtual methods (inheritance,
> etc.) you have to explicitly say âvirtualâ; otherwise a method call is
> implemented just like a regular C function call.
>
> Jens
My understanding is that a method call on a pointer is a dereference due
to this:
"If E1 has the type pointer to class X, then the expression E1->E2 is
converted to the equivalent form (*(E1)).E2"
It would appear that the * operator is part of the spec's definition.
Dou
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden