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 15:32:15 -0800
On Dec 2, 2015, at 12:00 PM, Jonathan Prescott < email@hidden> wrote:
In C++, “this” is a read-only (const) pointer setup during construction of the instance. Once an instance of a class is successfully created, the “this” pointer is guaranteed to be non-null for the lifetime of the instance.
No. It’s not a _part of_ the instance, it’s a _pointer to_ the instance. It only exists as an invisible parameter passed to a method.
For example, the method foo::method(int x) is internally implemented as a function [with a mangled name] that takes a parameter list (foo *this, int x). And calling f->method(1) is exactly like calling that function with parameter list (f, 1). Assuming the method is non-virtual. If it’s virtual, this gets more complicated. I tried out a couple of ways of trying to modify the “this" pointer of an existing class instance, and the clang and gcc compilers would not allow that operation to compile.
Again, ‘this’ doesn’t belong to the instance. It’s simply a function parameter.
Here’s a simple way to call a method with a NULL ‘this’:
struct foo { void method() { printf(“this = %p\n”, this); } }
foo *f = NULL; f->method();
—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