C++ concepts newbie Q's

So, what is the deal with dynamic_cast, static_cast and const_cast in UE4?
And do i need to know how to implement a template class/function in UE4?

In many cases you’re fine using C-style casts. The casts you mention are deep down in the syntax of C++ and even programmers with years of experience have trouble explaining them. So obviously, I can’t :slight_smile: But have a look in this StackOverflow-post for more info.

For your other question, the answer is yes, and no.

Template functions are awesome, and you should know how and where to use them. But do you need to know how to implement them? No. As with anything in UE4/C++, any knowledge is good knowledge, but it’s not always mandatory to make a game.

You can’t really do templates in UE4, at least not for UCLASSES. With pretty much everything in the engine inheriting from UObject you usually don’t need them anyways (there are still a lot of use cases though, particularly when writing math libraries that support multiple types or something).

As for casting, you usually don’t need to use anything but the engines ‘Cast()’ function. Dynamic_Cast isn’t supported by the engine since it’s not compiled with RTTI for performance reasons, and const_cast is just dirty anyway (usually). I’ve only had to use const_cast twice in UE4, and one of those times I switched to using ‘mutable’ instead.

As for C-Style casts, I only typically use that when casting between variables or something. Remember that these are NOT checked at compile time, so they can still fail. For example:

const int32 EnumAsInt = (int32)MyEnum::Value

const_cast: remove const
static_cast: the compiler can check if it is correct (if not you get a nullptr)
dynamic_cast: check must be done during runtime. Like a vector full of basepointers and you dynamic_cast to one of multiple derived classes. vector<Vehicle*>… you want a Car*.

Hi there!

I have a wiki on template functions here:

Wherever you'd use **dynamic_cast** you can use UE4's Cast&lt;&gt;, which is a dynamic cast, which means it will return nullpointer if the cast failed.


AMyActorClass* MyClassPtr = Cast<AMyActorClass>(GetOwner());


**static_cast** is dangerous because you can happily static_cast to something that absolutely is not what you are casting it to, which will likely cause a crash if you try to dereference the resulting pointer.


AMyActorClass* MyActorPtr = static_cast<AMyActorClass*>(AnyOlePointerWhichMayOrMayNotBeAMyActorClass);


**const_cast** lets you remove constness, as was mentioned above.



thank you, guys.
now that we are here, what other features from c++ 11 do you think aren’t necessary for UE4 and which UE4 features replace them(besides STL)? should i forbid slicing by deleting all move and copy operations for all base classes? also, what do you think about lambda functions and is it ok to always use nullptr instead of NULL? oh boy, i am on a spree.

Your first question, is a big question and I think experience will reply to this one. If you can do what you want with the Unreal API well do it, it will be less painful for multi-plateform support or replication.
Normally, lambda function is support, and yes use nullptr instead of NULL, useful link about : Use of NULL vs nullptr in UE4 C++ - UE4 AnswerHub

I hope this help you ! :slight_smile: