Implicit Conversion and the Explicit Keyword in C++

Posted By on May 18, 2019

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

Posted by Lewis Heart

This article has 27 comments

  1. i tried a lot but i could not understand .
    why Entity e ="cherno"; works when printentity("cherno"); failed……
    In the Entity e ="cherno"; also cherno is a const char arrray[7] so it must be first converted into std:: string and then into entity.two implicit conversions are needed.
    but how it works???

    i could see in comments that some say Entity e ="cherno"; is simply Entity e =Entity("cherno"); and only one implicit conversion is needed for std:: string.
    if that is the case if i make the constructor as explicit .
    then Entity e=22; is simply Entity e=Entity(22); must work but it fails….

  2. This is essentially what happens when you do int a = 2 i would assume instead of int a = int(2). nice.

  3. Bad casting syntax. Do not use the (Entity)22 syntax anymore. I think C++ should deprecate that syntax even though it would lose even more C compatibility. In this case, the right thing to do would've been static_cast<Entity>(22) or Entity(22). The latter preferably. The reason the C ones are evil is that they always work, even in situations where they should never work.

  4. Just Come back again to say my feeling: C++ looks like a really serious language (It do, sure) as C, and through many bells & whistles like this "explicit" keyword push it to a level C eventually not easy to achive (but sure still possible) & make it task a little bit like javascript right now (Since "ES2015" out & gigantic Javascript Transcompiler & Framework & Tooling movements)

  5. Hey Cherno I have a question. I know that's a old video but still. you do string& when you pass it in a function though you don't do int&. I know the difference between int& vs int though why most of the time we do string& and we don't do int&? Sorry if it is dumb.

  6. Entity a = "Cherno" gives compilation error in Visual studio 2017. Why Implicit conversion doesn't work?

  7. Shit, I wasted so much of my lifetime.. u're awesome man.
    where did u learn and how did u get a job at EA at this age?

  8. I nearly spit out my drink when I saw you write Entity a = "Cherno" because I was laughing at how silly it looked. Coming from Java and C++ is constantly surprising me xD

  9. One question, if you cant call printname("cherno") because "cherno" is not a string, why it is still valid for something like Entity a = "Cherno"?

  10. Who are these 2-3 people who follow this series up to video 40 to downvote all of them.
    I'm so curious.


Leave a Reply

Your email address will not be published. Required fields are marked *