const_cast In C++

Sdílet
Vložit
  • čas přidán 29. 08. 2024

Komentáře • 57

  • @CppNuts
    @CppNuts  Před 5 lety +6

    Hi everyone, Don't forget to hit LIKE and SUBSCRIBE button for more videos like this!!
    And this will help me a-lot.

  • @partha123dbr
    @partha123dbr Před 4 lety +11

    6:16 the value actually stored in a is 15 after it has been modified.

  • @bhupankgoswami5709
    @bhupankgoswami5709 Před 5 lety +12

    We have one more use of constant cast. If you have a constant member function inside a class then you will have read only permission, you can't update a variable value inside that function. In this situation we can use constant cast as well.

  • @IllumTheMessage
    @IllumTheMessage Před 2 lety

    Method of showing a common real world use case is very helpful. Good work, thank you.

  • @ravikiran6645
    @ravikiran6645 Před 6 lety +3

    in simple words, const_cast is used to cast away (remove) constness or volatility of variable/pointer/reference.

    • @CppNuts
      @CppNuts  Před 6 lety

      Yes that's correct but this sentence is confusing, because it doesn't allow to remove the constness of current variable/pointer/reference actually when you copy const to non const which is not allowed then it is useful.

    • @spicytuna08
      @spicytuna08 Před 6 lety +1

      cast away is wrong usage. in C,int typecast is done by (int)X where X can be a non int. By adding const to it, X becomes a const int. it makes a variable non mutable const int.

  • @abelashenafi6291
    @abelashenafi6291 Před 7 měsíci

    Thaks for the awesome explanation

  • @sids3194
    @sids3194 Před 5 měsíci +1

    How can we be sure that the third party library doesnt change the variable pointed to by the pointer? we may not have access to the code.

    • @aviralgoel5709
      @aviralgoel5709 Před 2 měsíci

      It will most likely be mentioned in the documentation of the function whether it changes the passed value or not.

  • @scutursula7536
    @scutursula7536 Před 5 lety +1

    Thank you very much :) You did a great job.

  • @gawarivivek
    @gawarivivek Před 2 lety

    This is amazing! Never knew this and could not even think of this ever. const_cast should have been prohibited for the use-case when the referenced object is const. Any specific reason, why it still allows it?

  • @ranjanrohit64
    @ranjanrohit64 Před 2 lety

    very well explained sir

  • @vallurijaganmohanrao72
    @vallurijaganmohanrao72 Před 2 měsíci

    Good

  • @VasuVandankodakandla975
    @VasuVandankodakandla975 Před 4 lety +2

    In first scenario, you have pointer to constant integer. But you are speaking as constant pointer. Correct it

  • @KrishnayanNivaas
    @KrishnayanNivaas Před měsícem

    voice quality in this video is not as usual like other videos. I find it difficult to understand some words you spoke in the video

    • @CppNuts
      @CppNuts  Před měsícem

      That time I did not have good set up.

  • @mausamyadav3522
    @mausamyadav3522 Před 3 lety

    Appreciated, good job !!!!!!!!!

  • @080hemant
    @080hemant Před 3 lety

    How we will know , if third party library is changing my pointer or not? (as you told, we don't know the implementation of third party library)

  • @ericbridge8419
    @ericbridge8419 Před 5 lety +4

    I can't even understand the first part lol
    Why are (a1) and (*d1) values different when they have the same memory address?

    • @CppNuts
      @CppNuts  Před 5 lety +1

      Actually const values was replaced at compile time only, so where ever you are using them as value you will only get the value which you assigned in the beginning when you made it const.

    • @ericbridge8419
      @ericbridge8419 Před 5 lety +2

      @@CppNuts Okay, so you can not change const values. But why does it "appear" to be changing then?

    • @AshishBudhiraja
      @AshishBudhiraja Před 4 lety

      I think this (a1) and (*d1) confusion wasn't removed satisfactorily.

  • @shihabahmed887
    @shihabahmed887 Před 5 lety +1

    6:09 here should be that i think, "instead of 10 now a1 which is this 0xFF address is actually holding 15" r8??

    • @krp1eee
      @krp1eee Před 4 lety

      this is not clear. he skips it.

  • @ghultul
    @ghultul Před 6 lety +4

    Time 6.20 min. The output of a1=10; seems ok as compiler optimized the code since the variable was declared as constant.
    But the second part is still unclear to me how "*d" is printing as 15.

    • @CppNuts
      @CppNuts  Před 6 lety

      Because *d1 is actually pointing to address of a1 and when we assigned *d1=15; //line number 16, then we actually changed a1 value. And if we are printing *d1 which is pointing to address of a1 and a1 has been changed to 15, we are getting 15.

    • @ghultul
      @ghultul Před 6 lety +1

      Yes, you are right, but my intention was to know since a1 is constant , it shouldn't have been modified.

    • @CppNuts
      @CppNuts  Před 6 lety +4

      the only thing which is GUARANTEED by C/C++ is that when you have declared some variable constant you can not change that value by using that same variable, but if we get the address of the place where that variable is storing that const value you can change it, like i did (so we are changing a1 using d1 and it is not violating any rule) :D.

    • @vaibhav1458
      @vaibhav1458 Před 6 lety +1

      CppNuts how to avoid this scenario of changing const variable using the address?

    • @CppNuts
      @CppNuts  Před 6 lety

      Changing const variable using its address is undefined behavior. I have clearly mentioned in video we should never use const_cast to remove const from variable so that we can change it.
      Rather it's use is to pass in some function which requires non const variable where as you have const variable.
      And i think you can not stop someone from changing const variable using const_cast. At least not for basic data types but you can stop in case of classes by simply not allowing to take address of your object. (This is my today's topic, don't miss that video :)
      In case someone interested : czcams.com/video/IAWoPgXtSNA/video.html
      And i am not sure about we can overload const_cast, would let you know if i will find something.

  • @junang5615
    @junang5615 Před 3 lety +1

    #2 is terrible advise! The whole point is that you don't know the implementation of the 3rd party lib method, hence casting away constness can easily get you into the gotcha you explained in example #1.

  • @aashishgoyal1436
    @aashishgoyal1436 Před 6 lety +1

    thanks a lot sir

  • @rahulkumaragrawal1394
    @rahulkumaragrawal1394 Před 6 lety +2

    This video is nice.. How about other casting ? I cudn't get.

    • @CppNuts
      @CppNuts  Před 6 lety

      Hi Rahul Kumar Agrawal, Yet to shoot them, you will get it soon, stay tuned. :D

  • @dayanitheebalaji7695
    @dayanitheebalaji7695 Před 4 lety

    you said d1 ultimately changed a1's address and a1 actually holding 10 not 15 , it is supposed to be 15(at a1)???

  • @rakhilsoman9299
    @rakhilsoman9299 Před 6 lety +1

    I believe this won't work if the 'const int a1' is a global variable? Is this because global const are stored in initialized DATA SEGMENT in cpp memory structure? Correct me if i am wrong.

    • @CppNuts
      @CppNuts  Před 6 lety

      Notice the point that it doesn't work on constant, it only works when original variable is non const and you get its adress as const now you want to remove const from this adress then it is defined behaviour, otherwise changing const thing is undefined behaviour.

    • @rakhilsoman9299
      @rakhilsoman9299 Před 6 lety +1

      const int a1 = 10;
      int main()
      {
      const int *b1 = &a1;
      int *d1 = const_cast(b1);
      *d1 = 15;
      cout

    • @CppNuts
      @CppNuts  Před 6 lety

      yes you are correct when we want to change global const then we won't be able to change it because it is stored in read only section but when it comes to local const variable the scenarios are different, local const variables are stored on stack so its ok you can change that, I think this will clear your doubt.

    • @rakhilsoman9299
      @rakhilsoman9299 Před 6 lety +1

      Yes. Thanks alot :-)

    • @spicytuna08
      @spicytuna08 Před 6 lety

      this code should create compilation error in my opinion. the line just had const_cast right before and the following line goes against it and assigns a value.

  • @yogeshasati6210
    @yogeshasati6210 Před 4 měsíci

    expalination is confusing

  • @amarjotsingh2016
    @amarjotsingh2016 Před 3 lety

    Your content is very good, but please improve the sound quality and speak slow...

    • @CppNuts
      @CppNuts  Před 3 lety

      Thanks..
      sure i will try.

  • @pendelaneelesh4130
    @pendelaneelesh4130 Před 3 lety

    constant_cast works fine even when the original variable is constant, I just ran the code and works fine for me.

    • @ChrisCox-wv7oo
      @ChrisCox-wv7oo Před 10 měsíci

      For you, this time, with this compiler.
      Its Undefined Behavior and should not be counted upon.

  • @abdullahkhalid9581
    @abdullahkhalid9581 Před 5 lety +1

    Explanation is not clear.

    • @CppNuts
      @CppNuts  Před 5 lety +1

      Which part?

    • @abdullahkhalid9581
      @abdullahkhalid9581 Před 5 lety

      The sound is not clear. I listened again with earphones in silence only then I understood it.