static_cast In C++

  Рет қаралды 94,738

CppNuts

CppNuts

Күн бұрын

JOIN ME
-----
KZbin 🎬 / @cppnuts
Patreon 🚀 / cppnuts
COMPLETE PLAYLIST
------------
C++ Tutorial For Beginners: • Introduction To C++
STL (Standard Template Library): • STL In C++
ThreadIng In C++: • Multithreading In C++
Data Structures: • Data Structure
Algorithms: • Binary Search
Design Patterns: • Factory Design Pattern...
Smart Pointers: • Smart Pointer In C++
C++14: • Digit Separator In C++
C++17: • std string_view in C++...
C++ All Type Casts: • static_cast In C++
INTERVIEW PLAYLIST
------------
C++ Interview Q&A: • Structural Padding & P...
C++ Interview Q&A For Experienced: • How delete[] Knows How...
Linked List Interview Questions: • Find Kth Node From Bac...
BST Interview Questions: • Search Element In Bina...
Array Interview Questions: • Reverse An Array
String Interview Questions: • Check String Is Palind...
Bit Manipulation Questions: • Find Set Bit In Intege...
Binary Tree Interview Question: • Invert Binary Tree
Sorting Algorithms: • Bubble Sort
C++ MCQ: • Video
C MCQ: • What printf returns af...
C Interview Questions: • Designated Initializat...
QUICK SHORT VIDEOS
-------------
C++ Short : • C++ Short Videos
C Short : • Shorts C Programming MCQ
In this video i have tried to explain static_cast in C++.
I have included 6 to 7 points to demonstrate the use static_cast in C++.
We use static_cast to check type casting at compile time. And it has more strict rules to check things at compile time.
In playlist we have covered all four types of typecast in c++ as follows:
static_cast in c++.
static_cast use in c++.
static_cast with example in c++.
what is the use of static_cast in c++.
#cpp #tutorial #programming #computerscience #softwareengineering

Пікірлер: 106
@CppNuts
@CppNuts 5 жыл бұрын
Hi everyone, Don't forget to hit LIKE and SUBSCRIBE button for more videos like this!! And this will help me a-lot.
@CppNuts
@CppNuts 5 жыл бұрын
Why should i help people, everyone should help them self in good ways. I don't want to make people handicapped by saying i will help you. Sorry i don't believe in this.
@saundaryadorle7545
@saundaryadorle7545 3 жыл бұрын
@@SO-dl2pv he already helped so much by providing excellent content for free..u can at least be grateful for that
@Kromush1995
@Kromush1995 6 жыл бұрын
Man, found your channel, and as a C++ developer, I must say that your videos are very good. Subscribed!
@CppNuts
@CppNuts 6 жыл бұрын
Thanks for such a nice comment!
@behindthescene4406
@behindthescene4406 4 жыл бұрын
Hey can u help me at 3:49 how obj= 20 is done as I know if we want to pass any value to initialise data member through object is obj(20) but here obj = 20 I didn't get it
@treyquattro
@treyquattro 4 жыл бұрын
4:17 the output is misleading - the first operation is actually "conversion constructor" but it's been scrolled off the top of the window already. This leads to misinformation in the video that the first operation is "conversion operator"
@AnuragsMusicChannel
@AnuragsMusicChannel 6 жыл бұрын
Amazing gyan bro! Keep it up. Just one thing at 4:19 the video output displayed "conversion operator" first. But actually conversion constructor should be called first(probably the output window size was the problem which scrolled up the first line). Anyways conversion constructor would be called thrice, once during creation of the object, second time during assigning 20 to obj and third time during execution of static_castobj.
@CppNuts
@CppNuts 6 жыл бұрын
yes you are correct there should be 5 output lines but here we get only 4 I didn't notice that 😀
@pramodchittara
@pramodchittara 6 жыл бұрын
3rd time during static_cast(30) not when static_castobj.
@bharatroxxx
@bharatroxxx 4 жыл бұрын
I came looking specifically for this.
@Byynx
@Byynx Жыл бұрын
I always go to your videos to get a more insight about a topic and you don't dissapoint.
@yourunclebob872
@yourunclebob872 5 жыл бұрын
This channel is a jackpot
@CppNuts
@CppNuts 5 жыл бұрын
Thanks for appreciation..
@巨龙之路
@巨龙之路 Ай бұрын
thank you ! 👍
@davithov
@davithov 4 жыл бұрын
You have Int obj(3); so conversion constructed is called first. You just need to scroll up to see that line printed.
@prashanthmj8
@prashanthmj8 3 жыл бұрын
Good explanation sir I love it
@CppNuts
@CppNuts 3 жыл бұрын
Thanks
@bhushanrane7459
@bhushanrane7459 6 жыл бұрын
Yes in point 5we should not use static_cast, In COM i.e in QueryInterface we are using reinterpret_cast to jump here to there
@saundaryadorle7545
@saundaryadorle7545 3 жыл бұрын
Kudos to u..super explaination always...just one clarification all kinds of pointer has same size but u mentioned couple of times char pointer has 1 byte size did u want to say char variable only
@CppNuts
@CppNuts 3 жыл бұрын
Yes i meant that only.
@abhi-s6f7m
@abhi-s6f7m 7 ай бұрын
Good class thank you
@beelover513
@beelover513 3 жыл бұрын
during point 2,I have a problem,the programm works only if i make a comment the two lines below, a)//return string str1(x); and b) //obj = static_cast(30);the output is, conversion constructor,conversion operator, conversion constructor,conversion operator.I can't find the solution of it.why it gives these two errors?
@GauravGupta-zw7hz
@GauravGupta-zw7hz 4 жыл бұрын
Thanks for this awesome video... which book do you recommend for CPP?
@CppNuts
@CppNuts 4 жыл бұрын
No books, read as much as u can online.
@kartikpodugu
@kartikpodugu Жыл бұрын
I got the 4th point mentioned, but can you elaborate what are the problems that occur if we do such casting using C style casting. You mentioned it is dangerous, but what is the danger?
@Indian-g3q
@Indian-g3q Жыл бұрын
in C style type casting will change variable value, if you put c=9 and after casting, value is changes from 9->0
@Nickel80
@Nickel80 5 жыл бұрын
In program 2, why don't we use the explicit keyword on the constructor so that it doesn't do implicit conversion?
@CppNuts
@CppNuts 5 жыл бұрын
But that is not the point here.
@adityalenka1499
@adityalenka1499 3 жыл бұрын
Very good explanation ❤️👍
@CppNuts
@CppNuts 3 жыл бұрын
Glad you liked it
@saradak5493
@saradak5493 5 жыл бұрын
Good explanation
@CppNuts
@CppNuts 5 жыл бұрын
Thanks..
@anandsenad6128
@anandsenad6128 6 жыл бұрын
thanks for making this video
@CppNuts
@CppNuts 6 жыл бұрын
Welcome dude checkout other casting videos as well.
@prishaphotography9063
@prishaphotography9063 7 жыл бұрын
Great work again...
@CppNuts
@CppNuts 7 жыл бұрын
Thanks man.. :D
@behindthescene4406
@behindthescene4406 4 жыл бұрын
Hey can u help me at 3:49 how obj= 20 is done as I know if we want to pass any value to initialise data member through object is obj(20) but here obj = 20 I didn't get it
@sumitnaik1990
@sumitnaik1990 5 жыл бұрын
Can you please explain about point 4 (private inheritance).
@takitachibana7717
@takitachibana7717 4 жыл бұрын
Thank you sir. well explanation
@CppNuts
@CppNuts 4 жыл бұрын
Thanks..
@varunsingh3825
@varunsingh3825 6 жыл бұрын
Good explanation but font is very dull, please solve the issue
@CppNuts
@CppNuts 6 жыл бұрын
Thanks, i will try..
@behindthescene4406
@behindthescene4406 4 жыл бұрын
Hey can u help me at 3:49 how obj= 20 is done as I know if we want to pass any value to initialise data member through object is obj(20) but here obj = 20 I didn't get it
@AmanKumar-fn7wz
@AmanKumar-fn7wz 5 жыл бұрын
@CppNuts Which GCC version allows implicit conversion from char* to int*, i am using GCC 8.2.0 and it does give compile error in int *p = (int*)&c;
@CppNuts
@CppNuts 5 жыл бұрын
Don't know man!!
@ArjanvanVught
@ArjanvanVught 5 жыл бұрын
g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0 int main() { char c = 'A'; int *p = (int*)&c; printf("c=%d, *p=%d ", c, *p); return 0; } $ ./a.out c=65, *p=892260161
@williamreji435
@williamreji435 3 жыл бұрын
Maybe the memory allocation changed .....if my memory serves me right.char under the hood was 4 bytes giving off c-style casting from char to int possible....(early but it's just random gibberish)
@temaelbouaazzaoui8709
@temaelbouaazzaoui8709 2 жыл бұрын
please I need an explanation about why shouldn't I use static_cast on privately derived classes
@_4p_
@_4p_ 8 ай бұрын
integer pointer 8 byte not 4 byte (for 64-bit machine)
@_4p_
@_4p_ 8 ай бұрын
And thanks for this tutorial, 🫡❤️
@vaibhavgupta8048
@vaibhavgupta8048 7 жыл бұрын
one more question how to access abstract class non-virtual function in c++ and what is significance of it in real time programming .please reply
@satyajeetkumarjha1482
@satyajeetkumarjha1482 2 жыл бұрын
i don't find repo of this on your github .Can you please paste the link ?
@rahul-patil
@rahul-patil 3 жыл бұрын
9:39 4 ) *static_cast avoid cast from derived to private base pointer* Could you please help me understand why it doesn't allow? In the below scenario, this casting could be useful. #include using namespace std; class Base{ public: int x; }; class Derived: private Base{ public: void print() { cout
@techytechwithgunjan9175
@techytechwithgunjan9175 6 жыл бұрын
#include using namespace std; int main() { // your code goes here float f; int a,b; a=5; b=2; f=static_cast(a/b); cout
@CppNuts
@CppNuts 6 жыл бұрын
a/b will produce int because a is int, use static_cast(a)/b to get the desired output.
@techytechwithgunjan9175
@techytechwithgunjan9175 6 жыл бұрын
thank you sir
@mentalthink1
@mentalthink1 5 жыл бұрын
Super like always...
@CppNuts
@CppNuts 5 жыл бұрын
Thanks..
@sarveshkaran9380
@sarveshkaran9380 5 жыл бұрын
For 1st one.. If suppose i entered a number then value to it will be assigned at runtime how will static_cast will operate that??
@CppNuts
@CppNuts 5 жыл бұрын
Static means static, it doesn't have anything to deal with at runtime and i know you know this, now coming to the question : if you pass number at run time then you must be having some data type for that number right?? so it will work on that data type not the number you have passed in.
@sabbulingineni
@sabbulingineni 5 жыл бұрын
can u pls share link for conversion operator and conversion constructor
@CppNuts
@CppNuts 5 жыл бұрын
Did i said in video that i have covered this topic? Because i am not able to find anything like that in my video list.
@sabbulingineni
@sabbulingineni 5 жыл бұрын
please do a video for conversion operator and conversion constructor...
@CppNuts
@CppNuts 5 жыл бұрын
kzbin.info/www/bejne/oGXMi2hmodVsga8
@spicytuna08
@spicytuna08 6 жыл бұрын
does this mean polymorphism occurs only when a class is inherited public?
@vaibhavgupta8048
@vaibhavgupta8048 7 жыл бұрын
good one ,thanks
@CppNuts
@CppNuts 7 жыл бұрын
Thanks Dude.
@comradepeter87
@comradepeter87 6 жыл бұрын
What IDE are you using?
@CppNuts
@CppNuts 6 жыл бұрын
It's Sublime Text Editor 3 (this is just editor) If you are from windows you may have to download mingw for gcc compiler.
@comradepeter87
@comradepeter87 5 жыл бұрын
@@CppNuts Yes I'm from windows and i already have installed MinGW for GCC for Code::Blocks. The background colour and text formatting in this -IDE- Editor attracted me. Thanks for help anyway!
@parameshreddy5137
@parameshreddy5137 5 жыл бұрын
Hi sir, Could you explain 4th point briefly?
@vaibhavgupta8048
@vaibhavgupta8048 7 жыл бұрын
how can we access a non-virtual function in derived class from a base class pointer in c++, please explain.
@CppNuts
@CppNuts 7 жыл бұрын
+Vaibhav Gupta, simple way is to typecast base pointer to derive pointer.
@vaibhavgupta8048
@vaibhavgupta8048 7 жыл бұрын
fine , using static_cast or dynamic_cast we can do , but i think static_cast is preferable.....,Thanks one more question how to access abstract class non-virtual function .please reply
@behindthescene4406
@behindthescene4406 4 жыл бұрын
Hey can u help me at 3:49 how obj= 20 is done as I know if we want to pass any value to initialise data member through object is obj(20) but here obj = 20 I didn't get it
@JeetkrishnaDas
@JeetkrishnaDas 4 жыл бұрын
to_string(x): identifier not found.. I get a compiler error
@glennmarkabalos6657
@glennmarkabalos6657 7 жыл бұрын
i want to learn more about void pointers... :)
@CppNuts
@CppNuts 7 жыл бұрын
No problem i will upload video for that.. :) Its Here: kzbin.info/www/bejne/qmrWZ2VuZb2Ea7M
@spicytuna08
@spicytuna08 6 жыл бұрын
what is wrong with (int) cast? why use such long word. the word static is now has different meaning in different context. now it has become one confusing F word. it is used in static class member. used in static function declaration. used in static variable local. compilation time now refers to static binding. there is static code analyzer. static has become confusing word.
@aliqyan9827
@aliqyan9827 4 ай бұрын
isn't static_cast explicit?
@spicytuna08
@spicytuna08 6 жыл бұрын
no wonder C++ is decreasing as a popular program language . why is it getting more F complicated? got your point but there got to be a better way.
@CppNuts
@CppNuts 6 жыл бұрын
When i saw this first time also felt the same way but when i started using them i found them so easy to remember and understand.
@spicytuna08
@spicytuna08 6 жыл бұрын
this video series is amazing. thanks to you. i am just frustrated with myself not understanding the real world application. there must be a good important reason for this.
@spicytuna08
@spicytuna08 6 жыл бұрын
What? why would anyone wants to convert an int obj to a string obj? i just don't see a practical application.
@CppNuts
@CppNuts 6 жыл бұрын
Generally when you pass your data to web browsers then everything is going as string. This is one of the example.
@EgorChebotarev
@EgorChebotarev 4 ай бұрын
not bad
@selvapriya4115
@selvapriya4115 5 жыл бұрын
gd class..
@CppNuts
@CppNuts 5 жыл бұрын
Thanks for the comment.
@subrajitbehera319
@subrajitbehera319 5 жыл бұрын
Change the background colour to white instead of black for IDE. Bcz letters are not visible properly.
@Byynx
@Byynx Жыл бұрын
Black color is still best. And best for our eyes too since has less blue light.
@quant-prep2843
@quant-prep2843 Жыл бұрын
lot of examples you do not explain, you just copy pasted the whole example code from geeks for geeks. really shame man
@gunjangosain8517
@gunjangosain8517 2 жыл бұрын
Person is speaking way too fast. I needed to change playback speed to 0.75
@NithinBelamkar
@NithinBelamkar 5 жыл бұрын
May be a good video for college students. Not an appropriate explanation for working professionals.
@CppNuts
@CppNuts 5 жыл бұрын
What is left plz comment, so working professional can get the benefits🤗👏👏.
@pramodkumarkar8153
@pramodkumarkar8153 5 жыл бұрын
too many adds , irritating
@CppNuts
@CppNuts 5 жыл бұрын
Sorry man!! can't help.
@rizwanahmedshaik8560
@rizwanahmedshaik8560 5 жыл бұрын
Use some more examples and try to explain slowly it seems like your your rushing somewhere
@CppNuts
@CppNuts 5 жыл бұрын
Oh i will try man, thank for the comment.
const_cast In C++
10:54
CppNuts
Рет қаралды 38 М.
reinterpret_cast In C++
14:33
CppNuts
Рет қаралды 45 М.
БОЙКАЛАР| bayGUYS | 27 шығарылым
28:49
bayGUYS
Рет қаралды 1,1 МЛН
Air Sigma Girl #sigma
0:32
Jin and Hattie
Рет қаралды 45 МЛН
Вопрос Ребром - Джиган
43:52
Gazgolder
Рет қаралды 3,8 МЛН
dynamic_cast In C++
10:07
CppNuts
Рет қаралды 69 М.
The 7 Levels of Math Symbols
14:03
The Unqualified Tutor
Рет қаралды 43 М.
Mutex In C++
16:22
CppNuts
Рет қаралды 113 М.
Germany | Can you solve this? | Math Olympiad
11:22
Master T Maths Class
Рет қаралды 649
Join And Detach In C++
17:15
CppNuts
Рет қаралды 70 М.
How delete[] Knows How Much  To Deallocate In C++?
12:24
CppNuts
Рет қаралды 71 М.
БОЙКАЛАР| bayGUYS | 27 шығарылым
28:49
bayGUYS
Рет қаралды 1,1 МЛН