In this entire code what "this" pointer refers to?

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.

#include "BasicClasses.h"
#include "LightSwitchCodeOnly.h"

ALightSwitchCodeOnly::ALightSwitchCodeOnly(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{
    DesiredIntensity = 3000.0f;

    PointLight1 = ObjectInitializer.CreateDefaultSubobject<UPointLightComponent>(this, "PointLight1");
    PointLight1->Intensity = DesiredIntensity;
    PointLight1->bVisible = true;
    RootComponent = PointLight1;

    Sphere1 = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("Sphere1"));
    Sphere1->InitSphereRadius(250.0f);
    Sphere1->AttachParent = RootComponent;

    Sphere1->OnComponentBeginOverlap.AddDynamic(this, &ALightSwitchCodeOnly::OnOverlap);        // set up a notification for when this component overlaps something
    Sphere1->OnComponentEndOverlap.AddDynamic(this, &ALightSwitchCodeOnly::OnOverlap);      // set up a notification for when this component overlaps something

}

void ALightSwitchCodeOnly::OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    if (OtherActor && (OtherActor != this) && OtherComp)
    {
        ToggleLight();
    }
}

void ALightSwitchCodeOnly::OnOverlapEnd(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
    if (OtherActor && (OtherActor != this) && OtherComp)
    {
        ToggleLight();
    }
}

void ALightSwitchCodeOnly::ToggleLight()
{
    PointLight1->ToggleVisibility();
}

And Happy new year 2015 to all of you! :))))
May God bless you all.

In C++ ‘this’ is a pointer that refers to the object itself. So in this case, this is a pointer to ALightSwitchCodeOnly.

No i dont think so.
First this refers to Pointlight1
Second this refers to Sphere1 and when the function Beginoverlap and Endoverlap are called this refers to Sphere1.
Am i correct guys??

is correct. In an object, this is a pointer that points to the object itself. It cannot be changed. In the class ALightSwitchCodeOnly, this always points to an object of type ALightSwitchOnly.

An possibly less confusing way to see how the this pointer works is to consider this part of your constructor:
ALightSwitchCodeOnly::ALightSwitchCodeOnly(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
DesiredIntensity = 3000.0f;
// … the rest of your code here.

Here the line
DesiredIntensity = 3000.0f;
can optionally be replaced with
this->DesiredIntensity = 3000.0f;

Qualifying DesiredIntensity with this-> gives you exactly the same results: In the current object, set the member variable DesiredIntensity to 3000.0f. The compiler is able to infer that you are referring to the current object when you’re assigning to the member variable DesiredIntensity within your class implementation, so the “this->” part is optional.

Hope this helps a bit.

was right.

The first two examples use:


FObjectInitializer::CreateDefaultSubobject(UObject * Outer, FName SubobjectName, bool bTransient = false)

Creating a subobject, “this” is being used to set the Outer object. You can read the first one like this:
Create a PointLightComponent subobject, make it’s owner this LightSwitchCodeOnly (i.e. this object), set its name as “PointLight1”

The 2nd two examples use:


AddDynamic(UserObject, FuncName)

Assigning a delegate function to be run when Sphere1 is overlapped, “this” is being used to set the object to run the delegate on. You can read the first like this:
When Sphere1 is overlapped call the “OnOverlap” function on this LightSwitchCodeOnly (i.e. this object)

@anonymous_user_85503c46: Please use


 tag or 

```php
 tag when posting source code. It will really help others to check your code. :)

Right now i've edited your post to add PHP code.