The lecture presents the following code as preventing changes to Actor:
for (const auto& Actor : OverlappingActors)
{
// Use Actor
}
This isn’t the case, since auto is deduced to be AActor* and const will apply to the outermost type (that is deduced by auto), or the pointer in this case. This means that the type of Actor is Actor* const& and Visual Studio will show this in a tooltip when hovering over Actor. The corresponding code to make Actor const, would be:
for (const auto* Actor : OverlappingActors)
{
// Use Actor
}
In this case, Actor is deduced to be AActor and the const applies to AActor, just as it would if you’d typed AActor instead of auto.
There’s a great talk by Scott Meyers about type deduction here.
I think your problem is that you are trying to point to a reference, basically this is impossible, so what is happening is VS is assuming you are trying to declare an AActor* called “const&”, so if you get rid of the & or the *, it should work, assuming this is what you are asking? BTW, i’m rather new to this syntax, so I might be completely misunderstanding what you are saying.
The talk by Scott Meyers looks like it’s great and well explained, but there are too many things I don’t know about C++ that seem to be prerequisites for that talk.
I have no idea about templates or L-type-references, so I guess I’ll ignore the whole thing for now and bookmark it for later.
But thanks for providing background infos!
One questions though:
The original code from Ben doesn’t crash or behave unexpectedly, so the only thing that is wrong with it is about the const declaration, right?
The reason you might want to use auto with a const, * or & is to change the type. So, you can do auto& r = s; and if s is a std::string, then r will be std::string&.
sorry for the late reply but thanks I will probably give it a try. but I am currently waiting for the revision in the Unreal course so I can start over fresh and hopefully focus more on the course and apply what I learned to make my projects better and hopefully more efficient