> On Dec 3, 2015, at 01:10, Anders Montonen <[email protected]> wrote:
> 
> 
>> On 03 Dec 2015, at 1:43, Jens Alfke <[email protected]> wrote:
>> 
>> If x is a value of class/struct X, and y is a non-virtual method, then x.y() 
>> turns into X::y(&x).
>> 
>> So putting the two together, if x is a pointer to X, then x->y() turns into 
>> X::y(x). There’s no pointer dereference involved, and no issue if x is NULL. 
>> All that happens is that in the implementation of y(), `this` will be NULL.
> 
> Is that standard-mandated behaviour or an optimization?

It is a legal optimization due to the fact that it is illegal to call a member 
function through a NULL pointer. i.e. no valid C++ code can ever result in 
“this" being NULL, so the compiler is free to assume that "this == NULL" is 
always false.

> If it is legal for any member function call to go via the this pointer, then 
> the check is illegal. As a practical consideration, in this example (modified 
> from the viva64 post to add a data member to the second base class), Xcode 
> 7.1.1 produces a non-null this pointer from a null pointer call:
> 
> #include <stdio.h>
> 
> class FirstBase {
>    int firstBaseData;
> };
> 
> class SecondBase {
>    int secondBaseData;
> public:
>    void Method()
>    {
>        if( this == 0 ) {
>            printf( "this == 0\n");
>        } else {
>            printf( "this != 0 (value: %p)\n", this );
>        }
>    }
> };
> 
> class Composed1 : public FirstBase, public SecondBase {
> };
> 
> int main()
> {
>    Composed1* object = 0;
>    object->Method();
> }
> 
> -a
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Xcode-users mailing list      ([email protected])
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/xcode-users/clarkcox3%40gmail.com
> 
> This email sent to [email protected]


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/xcode-users/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to