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: Jens Alfke <email@hidden>
- Date: Wed, 02 Dec 2015 23:03:37 -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 |
_______________________________________________
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