# C++ concepts newbie Q's

**URL:** <https://forums.unrealengine.com/t/c-concepts-newbie-qs/58699>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [March 18, 2016, 11:48pm UTC](https://forums.unrealengine.com/t/c-concepts-newbie-qs/58699 "2016-03-18T23:48:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon81437798](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon81437798](https://forums.unrealengine.com/u/anon81437798)\
**Post date:** [March 18, 2016, 11:48pm UTC](https://forums.unrealengine.com/t/c-concepts-newbie-qs/58699/1 "2016-03-18T23:48:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![MPalacios](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MPalacios](https://forums.unrealengine.com/u/MPalacios)\
**Post date:** [March 20, 2016, 2:17pm UTC](https://forums.unrealengine.com/t/c-concepts-newbie-qs/58699/2 "2016-03-20T14:17:51Z")

</div>

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 🙂 But have a look in [this StackOverflow-post for more info](http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast).

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.

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [March 20, 2016, 7:20pm UTC](https://forums.unrealengine.com/t/c-concepts-newbie-qs/58699/3 "2016-03-20T19:20:11Z")

</div>

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:

```auto

const int32 EnumAsInt = (int32)MyEnum::Value

```

---

<div class="post-metadata">

**Author:** ![BugSwat](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BugSwat](https://forums.unrealengine.com/u/BugSwat)\
**Post date:** [March 21, 2016, 3:27pm UTC](https://forums.unrealengine.com/t/c-concepts-newbie-qs/58699/4 "2016-03-21T15:27:19Z")

</div>

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\*.

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [March 22, 2016, 3:33am UTC](https://forums.unrealengine.com/t/c-concepts-newbie-qs/58699/5 "2016-03-22T03:33:13Z")

</div>

Hi there!

I have a wiki on **template functions** here:

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

````auto

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.

http://en.cppreference.com/w/cpp/language/const_cast

:)

Rama
````

---

<div class="post-metadata">

**Author:** ![anon81437798](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon81437798](https://forums.unrealengine.com/u/anon81437798)\
**Post date:** [April 8, 2016, 9:50pm UTC](https://forums.unrealengine.com/t/c-concepts-newbie-qs/58699/6 "2016-04-08T21:50:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jackblue](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jackblue/32/88706_2.png) [@Jackblue](https://forums.unrealengine.com/u/Jackblue)\
**Post date:** [April 11, 2016, 6:40am UTC](https://forums.unrealengine.com/t/c-concepts-newbie-qs/58699/7 "2016-04-11T06:40:11Z")

</div>

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++ - C++ - Unreal Engine Forums](https://answers.unrealengine.com/questions/1093/use-of-null-vs-nullptr-in-ue4-c.html)

I hope this help you ! 🙂
