C++ type casting with the static_cast operator [4]

Sdílet
Vložit
  • čas přidán 28. 08. 2024
  • Type casting enables you to manually convert data types. Learn how using the static_cast operator in this C++ tutorial for beginners using Visual Studio 2022.
    Type casting
    // Learn more:
    CH03: Expressions and Interactivity ⇒ • CH03: Expressions and ...
    // Join the new Programming for Everyone Community Forum:
    professorhank....
    // Consider supporting this channel in multiple ways
    paypal.me/hank...
    Bitcoin: 177wfkQwzXiC8o2whQMVpSyuWUs95krKYB

Komentáře • 27

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

    You explain everything so calmly, perfectly, it's def. easier to understand with you explaining it, thank you.

  • @j_naimi6031
    @j_naimi6031 Před 8 měsíci +2

    reading the book made it look hard for me, you made it clear in less than 3 minutes for me, thank you
    edit: u got me as a subscriber i love you bro keep doing your thing

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

    Thank you sir

  • @zhalynkabyken3765
    @zhalynkabyken3765 Před rokem +3

    Great stuff!

  • @user-ql7pw7ld1n
    @user-ql7pw7ld1n Před 6 měsíci

    I planned to do a the exact clever trick, but then u showed it was a mistake to wrap x/y inside static cast.. Im so stupid :P btw, very straight to the point of demonstration. I liked it

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před 6 měsíci

      Not stupid at all, just didn't know. I have been teaching C++, Python, Java for many years and there's still a ton I don't know. It's normal. Majority of programmers only use a fraction of whatever languages they know. 👍 Glad you got some value out of the video. Thanks for the comment.

  • @alimohsin3601
    @alimohsin3601 Před 3 měsíci

    Loved the explanation. Could you kindly make a video on dynamic casting as well? I'm having a hard time understanding why do we need dynamic casting in the first place.
    Lets say if their is a virtual function hello() in parent class and we wanna call the overridden hello of derived/child class, wouldn't this approach also achieve the result we want from dynamic cast.
    Parent * Ptr;
    Parent P_obj;
    Child C_obj:
    Child2 C2_obj;
    Ptr = &P_obj;
    Ptr = &C2_obj;

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před 3 měsíci

      In your example, yes because you are using inheritance. It's a form of implicit casting.
      An example of why you need casting:
      static_cast(5)/4
      to avoid integer division or
      int a[10];
      f.write(reinterpret_cast(a), sizeof(a))
      for writing to a binary file.

  • @_4p_
    @_4p_ Před 5 měsíci

    Thank you sir.

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

    how do you delete the whole word without deleting a certain character in that word?

  • @lickguitars1276
    @lickguitars1276 Před 11 měsíci

    i just converted float to int for modulating two numbers, pls take a look if its a good practice or not
    #include
    int main() {
    float num1, num2;
    std::cout > num1;
    std::cout > num2;
    std::cout

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před 11 měsíci

      Seems kind of pointless to me to store the input as floats since you convert them to ints anyway. Why not just make them floats in the first place thereby saving memory and the conversion operations?

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

    Thanks for this video.
    I have a question about multiple casting
    Here is an example:
    int array[5]{};
    array[0] = 1;
    array[1] = 2;
    Is it possible to keep the pointer arithmetic here as "int" but assign only a byte?
    So that "= 1" and "= 2" would be treated as a single byte/char instead of an int.

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

      You could store the 1 or 2 in a char variable, or you could use bitwise operators to store 1 in the high byte and 2 in the low byte of an unsigned short, for example.

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

      @@ProfessorHankStalica Thanks for the answer.
      I tried to assign it to a temporary variable of type char then assign it, the same result; the compiler still treats it as an int and so the other 6 bits are filled with zeros.
      What worked for me is this:
      *(char*)(int*)&array[0] = 1;
      *(char*)(int*)&array[1] = 2;
      Here the pointer arithmetic is maintained but only a single byte is copied but I'm not sure if this is safe way of doing it.

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

      Why not just make an array of chars?
      char nums[size];
      nums[0] = 1;
      1 is stored in a single byte then.

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

      @@ProfessorHankStalica Because I'm dealing with different types in the array. It's like a low level memory access like assembly stack where you can store 1 , 2 or 4 bytes.

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

      Sounds like you need an array of unions possibly:
      czcams.com/video/mKXFx600vR0/video.htmlsi=g5Fa7HqLf74RXLo8
      union Foo
      {
      char c;
      short s;
      int i;
      };
      Foo stuff[10];
      That would allow you to store 1, 2, or 4 bytes in each element of you array.

  • @muhammadamin5284
    @muhammadamin5284 Před rokem +1

    sir why you use
    cin.ignore() and cin.get() ??