. and -> operators

Hi,

I just had a quick question, I understand that the . and -> operations are used to access member functions from classes and all that. But that’s the thing aren’t they only used for classes and structs?. In this example in the Building Escape game Lecture 69 (ARCHIVED COURSE).

void UPositionReport::BeginPlay()
{
	Super::BeginPlay();
    
    FString ObjectName = GetOwner()->GetName();
	
    UE_LOG(LogTemp, Error, TEXT("I\'m a %s"), *ObjectName);
}

is GetOwner() a class?

You have to start somewhere… not sure if I want to use a ? or . to end this though lol.

You can’t just call GetName() because there would either be nothing there or something different.

No it is not a class, GetOwner() returns an AActor*

AActor* Owner = GetOwner();
FString ObjectName = Owner->GetName();

I’m sorry, but I don’t get it. If GetOwner isn’t a class then how come I can use the . and -> operators to access member functions?

It’s a function that returns a pointer too the type AActor which is a class. Refer to the code example above.

I think you’re confused by what’s happening in the background from the headers and # include uses.

As well as, it is also possible to write functions without a class (and in this case, the function returns a class object but is called without first typing a class), for one example the following can be done https://softwareengineering.stackexchange.com/questions/240336/only-functions-without-a-class-in-cpp-file-good-design-or-not

How do you ever know where to start? Generally, the 3rd party documentation should say how to do things, Google searching, and other times it should be possible to figure it out from within the code application and/or experimentation (though normally someone has already done it.)

Another example, you can create a function say GetOwnersName() that calls GetOwner()->GetName(); and returns the FString so that you just call a function GetOwnersName(); where no -> or . are needed.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms