Pointer of a pointer

This might be a very stupid question, but how can I do a pointer to a pointer? That is for example pointer x points to something, then pointer y points to pointer x, so when pointer x points to something else, pointer y will see the difference. How can this be done?

I might be wrong but if I do

AActor* pointer1 = *whatever*;
AActor* pointer2 = pointer1;
pointer1 = *different whatever*;

then pointer 2 will still point to whatever, right?

Oh, and doing AActor** went kinda crazy when I tried it.

A pointer to a pointer is rarely usefull, and if you use it, you have to use the address operator on the another pointer when you assign it:

AActor* pointer1 = *whatever*;
AActor** pointer2 = &pointer1; // & address operator

Sometimes you can see ** used for arrays that are filled with pointers. Like char** is actually a pointer to a pointer that points to a character like you can see it is done in the standard C main Function to handle multiple char arrays.

A pointer is a storage for a memory adress( meaning just a number), that additionally tells the compiler what kind of datatype he can expect at that adress. If there is another pointer to a datatype at that address and the original pointer tells this by indicating **, why not.

Your example above is really what you would do with a pointer most times, give it to some other class or use it as function parameter to access the original object. But take care, deleting the original object and then accessing it by a pointer that still has its address will crash.

But back to your original question, if you really need a reference to a pointer and it is no array of pointers, you might consider:

AActor* pointer1 = *whatever*;
AActor*& pointer2 = pointer1; // reference to the pointer

Here are a lot of examples when, why this pointet-to-pointer or reference-to-pointer could we useful and how: Pointer-to-Pointer and Reference-to-Pointer | CodeGuru

Crazy stuff? Welcome to C++

Did you try AActor** pointerPointerActor = &pointerActor;?

Thank you very much, that answers it. I’ve got another question though, can this be also done through blueprint? Thank you.

I tried to do this

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TEST")
		APickupableItem*& ActorPointerReferenceTest = PointedAtItem;

Without the UPROPERTY it worked perfectly, with it the engine just crashed when I started it. I guess what I want to do can be done with some kind of struct that has a pointer in it, right?

Hi Snowcrash,

that’s not possible, nor usefull imao with UProperties. Try it like that, only with a plain pointer:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TEST")
APickupableItem* ActorPointerReferenceTest;

then initialize it in the constructor if it is a component. Otherwise in BeginPlay. Or this here sounds like it is bound to what you look at. That would most likely be updated in the Tick function.

If the pointer does not get a value in the constructor right away it is usefull to initialize it as nullptr in the constructors initialization list, like this:

ATestActor::ATestActor(const FObjectInitializer& ObjectInitializer) :
	Super(ObjectInitializer),
	ActorPointerReferenceTest(nullptr)
{
	
}

void AGazeGuiElement::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);

	FCollisionQueryParams TraceParams(FName(TEXT("CurrentLookAtActorTrace")), false, this);
	
	const FVector Start = Camera->GetComponentLocation();
	const FVector End = Start + Camera->GetComponentRotation().Vector() * 100000;

	TArray<AActor*> ActorsToIgnore;
	ActorsToIgnore.Add(this);

	FHitResult HitData(ForceInit);
	UKismetSystemLibrary::LineTraceSingle_NEW(GetWorld(), Start, End, ETraceTypeQuery::TraceTypeQuery2, false, ActorsToIgnore, EDrawDebugTrace::None, HitData, true); // This will trace using the Camera Channel - ideal for VR visibility look at traces
   
	ActorPointerReferenceTest = Cast<APickupableItem>(HitData.Actor);

    // If your collision and stuff was set up correctly, and the actor you looked at was a APickupableItem, then the pointer should be something else as nullptr here...
}

Something like that.

** or *& are really only fore very special use cases that you should avoid and won’t need as a starter.