static_cast In C++

SdĂ­let
VloĹžit
  • čas přidĂĄn 20. 01. 2018
  • JOIN ME
    -----
    CZcams 🎬 / @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

Komentáře • 103

  • @CppNuts
    @CppNuts  Před 5 lety +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  Před 5 lety +3

      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 Před 3 lety

      @@SO-dl2pv he already helped so much by providing excellent content for free..u can at least be grateful for that

  • @Kromush1995
    @Kromush1995 Před 6 lety +11

    Man, found your channel, and as a C++ developer, I must say that your videos are very good. Subscribed!

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

      Thanks for such a nice comment!

    • @behindthescene4406
      @behindthescene4406 Před 4 lety

      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 Před 3 lety +10

    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"

  • @Byynx
    @Byynx Před 10 měsĂ­ci

    I always go to your videos to get a more insight about a topic and you don't dissapoint.

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

    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  Před 6 lety

      yes you are correct there should be 5 output lines but here we get only 4 I didn't notice that 😀

    • @pramodchittara
      @pramodchittara Před 5 lety

      3rd time during static_cast(30) not when static_castobj.

    • @bharatroxxx
      @bharatroxxx Před 4 lety +1

      I came looking specifically for this.

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

    This channel is a jackpot

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

      Thanks for appreciation..

  • @davithov
    @davithov Před 4 lety +1

    You have Int obj(3); so conversion constructed is called first. You just need to scroll up to see that line printed.

  • @beelover513
    @beelover513 Před 2 lety

    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?

  • @AmanKumar-fn7wz
    @AmanKumar-fn7wz Před 5 lety +1

    @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  Před 5 lety

      Don't know man!!

    • @ArjanvanVught
      @ArjanvanVught Před 4 lety

      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 Před 3 lety

      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)

  • @vaibhavgupta8048
    @vaibhavgupta8048 Před 6 lety

    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

  • @vallurijaganmohanrao72
    @vallurijaganmohanrao72 Před měsĂ­cem

    Good class thank you

  • @Nickel80
    @Nickel80 Před 4 lety +4

    In program 2, why don't we use the explicit keyword on the constructor so that it doesn't do implicit conversion?

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

      But that is not the point here.

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

    thanks for making this video

    • @CppNuts
      @CppNuts  Před 5 lety

      Welcome dude checkout other casting videos as well.

  • @GauravGupta-zw7hz
    @GauravGupta-zw7hz Před 3 lety

    Thanks for this awesome video...
    which book do you recommend for CPP?

    • @CppNuts
      @CppNuts  Před 3 lety

      No books, read as much as u can online.

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

    Great work again...

    • @CppNuts
      @CppNuts  Před 6 lety

      Thanks man.. :D

    • @behindthescene4406
      @behindthescene4406 Před 4 lety

      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

  • @kartikpodugu
    @kartikpodugu Před 9 měsĂ­ci

    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?

    • @ahmedzakir08
      @ahmedzakir08 Před 7 měsĂ­ci

      in C style type casting will change variable value, if you put c=9 and after casting, value is changes from 9->0

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

    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

  • @takitachibana7717
    @takitachibana7717 Před 3 lety

    Thank you sir. well explanation

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

    good one ,thanks

  • @sumitnaik1990
    @sumitnaik1990 Před 4 lety

    Can you please explain about point 4 (private inheritance).

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

    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  Před 5 lety +1

      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.

  • @satyajeetkumarjha1482
    @satyajeetkumarjha1482 Před 2 lety

    i don't find repo of this on your github .Can you please paste the link ?

  • @JeetkrishnaDas
    @JeetkrishnaDas Před 4 lety

    to_string(x): identifier not found.. I get a compiler error

  • @spicytuna08
    @spicytuna08 Před 6 lety

    does this mean polymorphism occurs only when a class is inherited public?

  • @saundaryadorle7545
    @saundaryadorle7545 Před 3 lety

    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  Před 3 lety +1

      Yes i meant that only.

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

    Good explanation

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

    how can we access a non-virtual function in derived class from a base class pointer in c++, please explain.

    • @CppNuts
      @CppNuts  Před 6 lety

      +Vaibhav Gupta, simple way is to typecast base pointer to derive pointer.

    • @vaibhavgupta8048
      @vaibhavgupta8048 Před 6 lety

      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 Před 4 lety

      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

  • @prashanthmj8
    @prashanthmj8 Před 3 lety

    Good explanation sir I love it

  • @adityalenka1499
    @adityalenka1499 Před 3 lety

    Very good explanation ❤️👍

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

      Glad you liked it

  • @parameshreddy5137
    @parameshreddy5137 Před 4 lety

    Hi sir, Could you explain 4th point briefly?

  • @mentalthink1
    @mentalthink1 Před 4 lety +1

    Super like always...

  • @rahul-patil
    @rahul-patil Před 3 lety +1

    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

  • @temaelbouaazzaoui8709
    @temaelbouaazzaoui8709 Před rokem

    please I need an explanation about why shouldn't I use static_cast on privately derived classes

  • @VivekYadav-ds8oz
    @VivekYadav-ds8oz Před 5 lety +2

    What IDE are you using?

    • @CppNuts
      @CppNuts  Před 5 lety

      It's Sublime Text Editor 3 (this is just editor)
      If you are from windows you may have to download mingw for gcc compiler.

    • @VivekYadav-ds8oz
      @VivekYadav-ds8oz Před 5 lety +3

      @@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!

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

    #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  Před 6 lety +4

      a/b will produce int because a is int, use static_cast(a)/b to get the desired output.

    • @techytechwithgunjan9175
      @techytechwithgunjan9175 Před 6 lety

      thank you sir

  • @_4p_
    @_4p_ Před měsĂ­cem

    integer pointer 8 byte not 4 byte (for 64-bit machine)

    • @_4p_
      @_4p_ Před měsĂ­cem

      And thanks for this tutorial, 🫡❤️

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

    i want to learn more about void pointers... :)

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

      No problem i will upload video for that.. :)
      Its Here: czcams.com/video/t9s6280XQ5Q/video.html

  • @sabbulingineni
    @sabbulingineni Před 4 lety +1

    can u pls share link for conversion operator and conversion constructor

    • @CppNuts
      @CppNuts  Před 4 lety

      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 Před 4 lety +1

      please do a video for conversion operator and conversion constructor...

    • @CppNuts
      @CppNuts  Před 4 lety

      czcams.com/video/j4iZ50lp9KM/video.html

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

    Good explanation but font is very dull, please solve the issue

    • @CppNuts
      @CppNuts  Před 5 lety

      Thanks, i will try..

    • @behindthescene4406
      @behindthescene4406 Před 4 lety

      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

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

    What? why would anyone wants to convert an int obj to a string obj? i just don't see a practical application.

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

      Generally when you pass your data to web browsers then everything is going as string. This is one of the example.

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

    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.

  • @selvapriya4115
    @selvapriya4115 Před 4 lety +1

    gd class..

    • @CppNuts
      @CppNuts  Před 4 lety

      Thanks for the comment.

  • @subrajitbehera319
    @subrajitbehera319 Před 4 lety +1

    Change the background colour to white instead of black for IDE. Bcz letters are not visible properly.

    • @Byynx
      @Byynx Před 10 měsĂ­ci

      Black color is still best. And best for our eyes too since has less blue light.

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

    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  Před 6 lety +1

      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 Před 6 lety +2

      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.

  • @pramodkumarkar8153
    @pramodkumarkar8153 Před 4 lety +1

    too many adds , irritating

    • @CppNuts
      @CppNuts  Před 4 lety

      Sorry man!! can't help.

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

    Use some more examples and try to explain slowly it seems like your your rushing somewhere

    • @CppNuts
      @CppNuts  Před 5 lety

      Oh i will try man, thank for the comment.

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

    May be a good video for college students. Not an appropriate explanation for working professionals.

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

      What is left plz comment, so working professional can get the benefits🤗👏👏.

  • @gunjangosain8517
    @gunjangosain8517 Před rokem

    Person is speaking way too fast. I needed to change playback speed to 0.75

  • @quant-prep2843
    @quant-prep2843 Před rokem

    lot of examples you do not explain, you just copy pasted the whole example code from geeks for geeks. really shame man