Getting confused with Structs

Im starting to getting a bit confused with the way structs are managed in c++. In the bulls and cows part, we learnt that we can initialize structs like so:

struct BullsNCows{
int32 Bulls;
Int32 Cows;
};
// then

BullsNCows Points = {10, 5};

In this case we will have a Points struct of the type BullsNCows that has 10 bulls and 5 cows.
Now in this lecture I see that the teacher is calling another struct like this:

FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());

I have the impression that it should’ve been

FCollisionQueryParams TraceParams{FName(TEXT("")), false, GetOwner()};

or

FCollisionQueryParams TraceParams = {FName(TEXT("")), false, GetOwner()};

are all of these options valid an in C++ the use of () or {} is almost interchangeable, or am I missing something here?

Another thing I don’t understand, I thought when you create an if statement with the name of a variable as a condition you were testing if the variable was a boolean returning true, but in this lecture the teacher writes:

if (ActorHit)

in this case ActorHit is an AActor, so what is the evaluation the if statement does?

Thanks!

They should be. Initialisation syntax in C++ is, by all accounts, insane
https://blog.tartanllama.xyz/initialization-is-bonkers/

Not quite. {} prefers constructors that take a std::initializer_list

#include <initializer_list>

class Example
{
public:
    Example(int); // 1
    Example(std::initializer_list<int>); // 2
};

int main()
{
    Example E1(1); // 1 gets called
    Example E2{ 1 }; // 2 gets called
}

I recommend only using {} when you need the one taking initializer_list, personally.
std::initializer_list is what’s being used here

TArray<int32> Bob{ 1, 2, 3, 4 };

Also personally, I never use = for initialising variables other than the primitive types.

if (Pointer) is the same as if (Pointer != nullptr).
From the standard:

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer-to-member type can be converted to a prvalue of type bool.

A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

- (emph, mine) https://eel.is/c++draft/conv.bool

2 Likes

Thank you so much for taking the time to go through all these question, really thorough!

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

Privacy & Terms