I want to do some logic of AActor in nested object and i need to pass this actor by reference to make it work. Unfortunatelly, i don't see a way how to pass "this" when "&" is used. Can someone explain me what's going on?
ATestActor.h
class UHelper;
UCLASS()
class PROJECT_API ATestActor : public AActor
{
GENERATED_BODY()
public:
ATestActor();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
UPROPERTY()
UHelper* Helper;
};
UCLASS()
class PROJECT_API UHelper : public UObject
{
GENERATED_BODY()
public:
void Help(ATestActor* &TestActor)
{
// Do some stuff with testActor
}
};
ATestActor.cpp
//other stuff
void ATestActor::BeginPlay()
{
Super::BeginPlay();
Helper = NewObject<UHelper>();
//How can i pass "this" to the method?
//Helper->Help(&this);//incorrect
}
&this
creates a pointer to a pointer. your function Help
accepts a reference to a pointer. calling Helper->Help(this);
should work since this
is already a pointer, then we pass the ptr by reference when calling Help
.
edit:
actually i forgot this
is a prvalue expression and prvalues cant bind to non const lvalue references.
you could do the following to get an lvalue of the this
ptr.
auto* obj = this;
Helper->Help(obj);