Download

Noob c++ q: Why am I not using -> to access member function of UWhateverComponent * in a function?

I am getting a UStaticMeshComponent and accessing its member function GetPhysicsLinearVelocity(), in the .h, I declared the pointer as such:


UPROPERTY(Category = Mesh, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
	class UStaticMeshComponent* NeonPhys_MainPhysBodyComponent;

and in the class constructor, I CreateDefaultSubobject into it:


Blah_MainPhysBodyComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Blah_MainPhysBodyComponent"));

Then I call it in a function:


void APawn_BlahBlah::BlahBlahGetVelocities(blahblah..., UStaticMeshComponent Blah_MainPhysBodyComponent)
{
	blahLinVel= Blah_MainPhysBodyComponent.GetPhysicsLinearVelocity();
	
	return;
}


Now what I don’t quite get as a c++ noob is why the accessor is ‘.’ and not ‘->’? Is this not a pointer UStaticMeshComponent and thus requires the -> ? And even if I deref the pointer in the arguments as such: UStaticMeshComponent &Blah_MainPhysBodyComponent, I still need to use Blah_MainPhysBodyComponent.memberwhatever();

Thank you for your noob patience!

pointer are accessed by ->

references and values/structs are accessed by point ..

Also, note that you have 2 different declarations one as a function argument, and one as a local variable to the class instance.

One declaration contains a * (that is how yu declare a pointer).

Will suguest to search ehre or on the web a tutorial on pointers, and declaration of variables (for classes, local variables and arguments)

Thank you!

To clear up tyoc213’s answer, you are declaring the variable in your class as a pointer, and setting it correctly via CreateDefaultSubobject, but your function takes the object itself, rather than a pointer (indicated by the lack of a * preceding the variable name). This will cause problems, because passing the object by value instead of by pointer(or reference) implicitly copies the object. This means that all changes you make will not affect the original object, but rather the local copy being deleted at the end of the function.

Change “UStaticMeshComponent Blah_MainPhysBodyComponent” to “UStaticMeshComponent* Blah_MainPhysBodyComponent”, and the “.” accessor to “->”, and you should be fine.

EDIT: Although really, you should find a name for your function variable that isn’t the name of the class variable.

Thank you so much, very enlightening!