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

  Рет қаралды 5,716

Professor Hank Stalica

Professor Hank Stalica

Күн бұрын

Пікірлер: 32
@haarkee9366
@haarkee9366 9 ай бұрын
You explain everything so calmly, perfectly, it's def. easier to understand with you explaining it, thank you.
@ProfessorHankStalica
@ProfessorHankStalica 9 ай бұрын
Glad it was helpful!
@j_naimi6031
@j_naimi6031 Жыл бұрын
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
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Awesome, thank you!
@surojitnath3336
@surojitnath3336 Ай бұрын
That's one of simple explanation I have seen, Thanks for the video :)
@ProfessorHankStalica
@ProfessorHankStalica Ай бұрын
Glad it was helpful!
@zhalynkabyken3765
@zhalynkabyken3765 Жыл бұрын
Great stuff!
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Glad you think so!
@seven-xl2225
@seven-xl2225 Ай бұрын
you solved my life problem , THX bro
@Madhav-uq1tm
@Madhav-uq1tm 2 ай бұрын
Thank you sir❤
@ProfessorHankStalica
@ProfessorHankStalica 2 ай бұрын
So nice of you
@oussamabouallati
@oussamabouallati 5 ай бұрын
Thank you sir
@_4p_
@_4p_ 9 ай бұрын
Thank you sir.
@ProfessorHankStalica
@ProfessorHankStalica 9 ай бұрын
Welcome!
@TheRawi
@TheRawi 6 ай бұрын
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 6 ай бұрын
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 6 ай бұрын
@@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 6 ай бұрын
Why not just make an array of chars? char nums[size]; nums[0] = 1; 1 is stored in a single byte then.
@TheRawi
@TheRawi 6 ай бұрын
@@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 6 ай бұрын
Sounds like you need an array of unions possibly: kzbin.info/www/bejne/o3y7d6tsZZWpiJIsi=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.
@alimohsin3601
@alimohsin3601 7 ай бұрын
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 7 ай бұрын
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.
@visitor_t-w3p
@visitor_t-w3p 10 ай бұрын
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 10 ай бұрын
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.
@muhammadamin5284
@muhammadamin5284 Жыл бұрын
sir why you use cin.ignore() and cin.get() ??
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Here you go: kzbin.info/www/bejne/hHqqmZ2ojLGKeNE
@lickguitars1276
@lickguitars1276 Жыл бұрын
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 Жыл бұрын
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?
@maknify4538
@maknify4538 5 ай бұрын
how do you delete the whole word without deleting a certain character in that word?
@ProfessorHankStalica
@ProfessorHankStalica 5 ай бұрын
Question is too broad, so the answer is "it depends".
C++ Multiple and Combined Assignment Explained: Tips and Examples [5]
4:59
Professor Hank Stalica
Рет қаралды 639
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН
Casting in C++
13:25
The Cherno
Рет қаралды 190 М.
C++ STL Tutorial: maps
19:32
Professor Hank Stalica
Рет қаралды 1 М.
you will never ask about pointers again after watching this video
8:03
ASU CS Senior Attempts to Solve LeetCode Daily Challenge Live
4:24
Dynamic Casting in C++
13:59
The Cherno
Рет қаралды 119 М.
C++ Pass by Value, Reference, Pointer Explained
9:33
Caleb Curry
Рет қаралды 46 М.
How to use binary files [an updated C++ tutorial]
19:18
Professor Hank Stalica
Рет қаралды 19 М.
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН