Implicit Conversion and the Explicit Keyword in C++

The Cherno · Intermediate ·8y ago
Skills: API Design60%

Key Takeaways

The video covers implicit conversion and the explicit keyword in C++, demonstrating how implicit conversion can lead to unexpected behavior and how the explicit keyword can be used to prevent it.

Full Transcript

hey what's up guys my name is the cheddar waving my arms around welcome back to my say plus plus series today we're going to talk all about implicit construction C++ implicit conversions as well as what the explicit keyword actually means so implicit conversion plus it concern what am I talking about implicit means kind of without you explicitly telling it what to do so it kind of automatic that's what that work kind of means in this context and what's the bus bus is actually allowed to do what the compiler is allowed to do is perform one implicit conversion on your code because if I kind of start using one data type as another and a conversion between the two exists C++ will actually implicitly perform that conversion without you having to cast it or anything like that whoever video and casting very soon casting is what we do to kind of convert from one data type to another so this is best shown with an example so let's take a look I'm going to write a class here it's going to be called entity it's going to have a private string name you guys know the drill I've done this before I'm also going to add an age to this will create an NC constructor which takes in a name I write Const St string reference name Palestine name to name here also create another constructor which takes in an age I'll sign name here to be unknown an age to be age I should definitely also assign age over here we'll set age to be negative one meaning that it's invalid basically no age was supplied okay nice so we have two constructors here which are pretty simple so the normal way that you would probably create these objects is by writing something like entity a can maybe give you a name like sugar and GB maybe giving this one in agents 222 that's actually how old I am for those of you wondering because I get that question a lot for some reason and you would kind of go about your day and not think twice and this looks pretty simple maybe if you want to use equals you would write your code like this and it kind of looks normal this is how most people use objects and how much people instantiate objects but what you can do which a lot of people don't know is just basically say that n CA is equal to Turner or NTP is equal to 22 now this is a little bit weird because well first of all you can't do this in other languages like Java or C sharp but second of all entity B equals 22 what is interesting integer I mean North is got string and it's got a name but I can assign 22 what's going on here this is called implicit conversion or illicit construction it's implicitly converting that 22 into an axiom constructing an entity out of it because there's a constructor fantasy which takes in an integer age right and there's a constructor fantasy which takes in a name check another example where you might say this is maybe you have a print st function which takes in an entity and then maybe does some cool printing stuff what you can actually do here is call that function with 22 right and that looks weird because we'll hang on a minute we don't have an overload for print and T which takes in an integer or something like that we've just got one that takes in an entity but remember as far as c++ is concerned 22 can be converted into an entity because you can call this constructor and suddenly from 22 which is the only parameter you've made an entity now watch what happens when I try and call this print entity function with Cherno you would kind of assume it to work right because this did no it doesn't work and the reason it didn't work is because this churner string isn't actually an STD string it is a char array it's a Const our array of seven characters we've got shown on and then an old termination character if you guys don't know how strings work link up their link in the description below thanks everywhere go check that video out a very very useful stuff so in order for this to work C++ would actually have to do two conversions one from a Const RA into a string and then one from a string to an entity and it's only allowed to do one implicit conversion so for this to work we would have to either wrap this inside a constructor like this the string and suddenly this works fine or we could even wrap it just in an entity and that would work as well because in this case it would be implicitly converting this string into a string into an STD string into a standard string and then that will be pushed into the HD constructor and constructor available okay cool that's impressive construction pretty cool stuff can help simplify your code a lot personally I try to avoid it as much as possible except for some some cases when you're kind of assigning it can get it can kind of just simply oh so you're not wrapping it with constructors all the time but in general I wouldn't be assigning this entity b22 like this I personally would write code like this instead because in my opinion it just looks a bit more clear now let's talk about what the explicit keyword is because it's very very relevant to this explicit disables the simplistic analogy the explicit keyword is something that you put in front of a constructor and if you write an explicit constructor it means that no no no no no implicit conversions this constructor must explicitly be called if you want to construct this entity object with an integer for example so let's go back to having this implicit conversion here if I go up here and I change this int age constructor to be explicit by sticking explicit out the front you'll see that these birds suddenly fail I cannot do that anymore if I want to do this I need to either write code like that or explicitly cast it to an entity by doing this we'll talk about passing in another video it'll be a link up there and in description below as well all we can of course just call that entity constructor like this as well okay so those are kind of our options but we can no longer just implicitly call code like this and expect it to work it's not going to happen and of course if we write explicit for this constructor which takes in string then of course this is going to fail as well not this one just yet because we are still of course actually calling the entity constructor but everything else that is implicit like this one fails and that is really the only function of that explicit keyword it's for when you want your constructors to be explicitly called instead of allowing the sibyl's compiler to implicitly convert any integer into entity by just basically calling this constructor every time you try and do that okay so that's what implicit is that's what explicit is hopefully this plays some stuff up as for when you want to use this I use explicit sometimes for things like math libraries if I really don't want to be comparing numbers to vectors time and I want to just ensure that my code is as safe as possible honestly I don't find myself using it too often when you're writing low-level rappers or things like that it can come in handy and can prevent you from accidentally casting things and causing you the performance issues or buzz will kind of maybe talk about specific examples in the future if we run into them but it's one of those things that you probably don't need to worry about too much just be aware of this keyword and what it can do for you and the big the big take away I think from this video is just be aware that implicit construction exists hope you guys enjoy this video if you did you hit that like button you can also help support this series and make it even better by going to patreon home for such the channel but they don't reduce support the series you do get some pretty cool rewards such as being able to see these exits early basically as soon as I'm done editing them as well as contribute to the discussion of what goes into these videos in the future if there's something in this video that you didn't understand you can leave a comment below and I'll try and reply to as many as I can but I also have a discourse above link in the description below in which you could talk about this kind of stuff with with a handful of people I think there's like 200 people in the 700 race that's pretty cool definitely join up their link in description below and I'll see you guys in the next video goodbye [Music] [Music]

Original Description

Patreon ► https://patreon.com/thecherno Twitter ► https://twitter.com/thecherno Instagram ► https://instagram.com/thecherno Discord ► https://discord.gg/R2t97wf Series Playlist ► https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb Gear I use: ----------------- BEST laptop for programming! ► http://geni.us/pakTES My FAVOURITE keyboard for programming! ► http://geni.us/zNhB FAVOURITE monitors for programming! ► http://geni.us/Ig6KBq MAIN Camera ► http://geni.us/t6xyDRO MAIN Lens ► http://geni.us/xGoDWT Second Camera ► http://geni.us/CYUQ Microphone ► http://geni.us/wqO6g7K
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from The Cherno · The Cherno · 0 of 60

← Previous Next →
1 3D Game Programming - Episode 1 - Window
3D Game Programming - Episode 1 - Window
The Cherno
2 3D Game Programming - Episode 2 - Game Loop
3D Game Programming - Episode 2 - Game Loop
The Cherno
3 3D Game Programming - Episode 3 - Arrays
3D Game Programming - Episode 3 - Arrays
The Cherno
4 3D Game Programming - Episode 4 - Drawing Pixels!
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
5 3D Game Programming - Episode 4.5 - How Rendering Works
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
6 3D Game Programming - Episode 5 - Playing with Pixels!
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
7 3D Game Programming - Episode 6 - Performance Boosting
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
8 3D Game Programming - Episode 7 - FPS Counter
3D Game Programming - Episode 7 - FPS Counter
The Cherno
9 3D Game Programming - Episode 8 - Alpha Support and More
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
10 3D Game Programming - Episode 9 - Beginning 3D
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
11 3D Game Programming - Episode 10 - Floors and Animation
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
12 3D Game Programming - Episode 11 - Rotation
3D Game Programming - Episode 11 - Rotation
The Cherno
13 3D Game Programming - Episode 12 - User Input
3D Game Programming - Episode 12 - User Input
The Cherno
14 3D Game Programming - Episode 13 - Render Distance Limiter!
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
15 3D Game Programming - Episode 14 - Basic Mouse Movement
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
16 3D Game Programming - Episode 15 - Textures + More!
3D Game Programming - Episode 15 - Textures + More!
The Cherno
17 3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
18 3D Game Programming - Episode 16.5 - Exporting Runnable Jars
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
19 3D Game Programming - Episode 17 - Small Adjustments + Birthday!
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
20 3D Game Programming - Episode 17.5 - Creating an Applet
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
21 3D Game Programming - Episode 18 - The Beginning of Walls
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
22 3D Game Programming - Episode 18.1 - A Few More Things
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
23 Episode 18.5 - Creating an EXE File in Java
Episode 18.5 - Creating an EXE File in Java
The Cherno
24 3D Game Programming - Episode 19 - Rendering Walls
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
25 3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
26 3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
27 3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
28 3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
29 3D Game Programming - Episode 24 - Making Our Launcher Work
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
30 3D Game Programming - Episode 25 - Writing and Reading Files
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
31 3D Game Programming - Episode 26 - Custom Resolutions
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
32 3D Game Programming - Episode 27 - Decorating the Launcher
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
33 3D Game Programming - Episode 28 - Continuing our Custom Launcher!
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
34 3D Game Programming - Episode 29 - Launching The Game
3D Game Programming - Episode 29 - Launching The Game
The Cherno
35 3D Game Programming - Episode 30 - Colour Processing In-Depth
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
36 3D Game Programming - Episode 31 - Sprites!
3D Game Programming - Episode 31 - Sprites!
The Cherno
37 3D Game Programming - Episode 32 - Sprite Mapping
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
38 3D Game Programming - Episode 33 - High Resolution Rendering
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
39 3D Game Programming - Episode 34 - Entities
3D Game Programming - Episode 34 - Entities
The Cherno
40 Genesis - My Game for Ludum Dare 24
Genesis - My Game for Ludum Dare 24
The Cherno
41 Vlog + Ludum Dare Results
Vlog + Ludum Dare Results
The Cherno
42 Game Programming - Episode 1 - Resolution
Game Programming - Episode 1 - Resolution
The Cherno
43 Game Programming - Episode 2 - Threads
Game Programming - Episode 2 - Threads
The Cherno
44 Game Programming - Episode 3 - Game Loop
Game Programming - Episode 3 - Game Loop
The Cherno
45 Game Programming - Episode 4 - Window
Game Programming - Episode 4 - Window
The Cherno
46 Episode 5 - Buffer Strategy
Episode 5 - Buffer Strategy
The Cherno
47 Game Programming - Episode 6 - Graphics Initialized
Game Programming - Episode 6 - Graphics Initialized
The Cherno
48 Game Programming - Episode 7 - Buffered Image and Rasters
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
49 Game Programming - Episode 8 - The Screen Class
Game Programming - Episode 8 - The Screen Class
The Cherno
50 Game Programming - Episode 9 - Rendering Pixels
Game Programming - Episode 9 - Rendering Pixels
The Cherno
51 Game Programming - Episode 10 - Clearing the Screen
Game Programming - Episode 10 - Clearing the Screen
The Cherno
52 Game Programming - Episode 11 - "Out of Bounds, Baby!"
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
53 Game Programming - Episode 12 - Negative Bounds
Game Programming - Episode 12 - Negative Bounds
The Cherno
54 Game Programming - Episode 13 - Timer
Game Programming - Episode 13 - Timer
The Cherno
55 Game Programming - Episode 14 - FPS Counter
Game Programming - Episode 14 - FPS Counter
The Cherno
56 Episode 15 - Tiles
Episode 15 - Tiles
The Cherno
57 Game Programming - Episode 16 - The Map
Game Programming - Episode 16 - The Map
The Cherno
58 The Walls 2 - Minecraft PvP Survival Map
The Walls 2 - Minecraft PvP Survival Map
The Cherno
59 Game Programming - Episode 17 - Key Input
Game Programming - Episode 17 - Key Input
The Cherno
60 Game Programming - Episode 18 - Controlling The Map
Game Programming - Episode 18 - Controlling The Map
The Cherno

This video teaches C++ developers how to use the explicit keyword to prevent implicit conversion and ensure safer code. It covers the basics of implicit conversion, constructor overloading, and how to apply the explicit keyword to avoid unexpected behavior.

Key Takeaways
  1. Create a class with a constructor that takes in a parameter
  2. Use implicit conversion to assign a value to an object
  3. Apply the explicit keyword to a constructor to prevent implicit conversion
  4. Call a function with an object as an argument when the function expects a different type
  5. Wrap a string in a constructor and use the explicit keyword
💡 The explicit keyword can be used to prevent implicit conversion and ensure safer code in C++
Up next
George Hotz | Programming | tinygrad, starting on CLOUD=1 | Part 2
george hotz archive
Watch →