Casting in C++

Sdílet
Vložit
  • čas přidán 28. 08. 2024
  • Patreon ► / thecherno
    Twitter ► / thecherno
    Instagram ► / thecherno
    Discord ► thecherno.com/...
    Series Playlist ► thecherno.com/cpp
    Thank you to the following Patreon supporters:
    - Dominic Pace
    - Kevin Gregory Agwaze
    - Sébastien Bervoets
    - Tobias Humig
    - Peter Siegmund
    - Kerem Demirer
    Gear I use:
    -----------------
    BEST laptop for programming! ► geni.us/pakTES
    My FAVOURITE keyboard for programming! ► geni.us/zNhB
    FAVOURITE monitors for programming! ► geni.us/Ig6KBq
    MAIN Camera ► geni.us/t6xyDRO
    MAIN Lens ► geni.us/xGoDWT
    Second Camera ► geni.us/CYUQ
    Microphone ► geni.us/wqO6g7K

Komentáře • 177

  • @naoufal450
    @naoufal450 Před 6 lety +110

    After watching accidentally a video of yours, I found you an awesome teacher and I've completed the whole series in a matter of days, never gets boring.
    You really motivated me to seriously learn this language (I did know most basic things but was lazy and stopped), I hope you continue doing videos, trust me it's going to be a very valuable library.

  • @jaesiraki84
    @jaesiraki84 Před 3 lety +28

    Such a modest energy and I can actually understand what you're saying. Thanks!

    • @MaySmithereen
      @MaySmithereen Před 2 lety +1

      Some people explain in a very gate-keeper type of way :( Really glad people like him exist!

  • @sonulohani
    @sonulohani Před 6 lety +140

    You explain very well. And youre the only one who doesn't use mac book.

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

      He is the only C++ programmer who does not use a mac? It should not matter anyway :)

    • @lukedk4614
      @lukedk4614 Před 4 lety +13

      @@groberti It does matter once you get into platform specific implementations. Obviously someone in a mac won't use things like the Win32 or DirectX / Direct3D (well not officially anyways). But yeah, for this topic specifically it doesn't matter.

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

      @@lukedk4614 Well, obviously. Or you could use a cross-platform framework (like OpenGL) and then it does not matter as much. The op is not explicit enough btw, are most other people who teach C++ use Mac or do programmers in general? I do think that the Mac is the most convenient platform if you are developing "platform independent" things or if you're developing for the Mac. Or even if you are developing for multiple platforms, but then you'll need another computer with Windows / Linux as well anyway

    • @smileynetsmileynet7922
      @smileynetsmileynet7922 Před 4 lety +3

      @@groberti He's not the only one that doesn’t use a MacBook. Lots of people dont use them. For me, they are simply too expensive. But I use both Windows and Linux all the time.
      Now, for the bleeding edge, Linux is the one that gets features first for my languages, such as PNF.

    • @FedJimSmith
      @FedJimSmith Před 2 lety +1

      @@groberti he's a game developer and the game he develops run on windows not mac or linux, so obviously he will use Visual Studio on Windows

  • @leixun
    @leixun Před 4 lety +53

    *My takeaways:*
    1. Implicit and explicit casting - C style 1:53
    2. Static cast - C++ style 4:15
    3. Why we want to use C++ style cast 4:44
    4. Dynamic cast - C++style 8:44

  • @KostasOreopoulos
    @KostasOreopoulos Před 5 lety +43

    static_cast also helps to avoid removing constantness that can be easily done through c-style casting

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

      @Ameer Leonel BOT

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

      @Jon Anderson bot?

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

      @@hammielover225 Yes these always come in pairs, one claims to hack his gf's/sister's Instagram, and the other says he'll try it out.

    • @hammielover225
      @hammielover225 Před 3 lety +2

      @@fppt1555 I know that

  • @xonxt
    @xonxt Před 5 lety +7

    I've read somewhere, that it's actually faster and more efficient to use the C++-style casting, because if you use the "old style" casting, the modern compiler will try to attempt every c++-style casting in turn (const -> static -> reinterpret) and return the result of the first successful cast. But if you use one of them explicitly yourself, you just do it directly, without extra steps.

    • @theshermantanker7043
      @theshermantanker7043 Před rokem +4

      It's faster at compile time, not when actually running

    • @ataladin87
      @ataladin87 Před rokem

      @@theshermantanker7043 difference???

    • @oreoisacat6297
      @oreoisacat6297 Před rokem

      @@ataladin87 it will compile faster but when you run it, c style would be faster.

  • @itsmaxim01
    @itsmaxim01 Před 9 měsíci +3

    another drawback of (C)style casts is that they will attempt every cast type until one works, meaning the result won’t necessarily be what you expect

  • @chinmaydas4053
    @chinmaydas4053 Před 5 lety +21

    Sir, now We need more advanced c++ series from a great guy like you,many many thanks👍🙏🙏..

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

    It's amazing. I have been using C++ for half a decade and written quite a few rather complex programs, yet there are still features I didn't know, lol.

    • @ahmedbugti3152
      @ahmedbugti3152 Před 4 lety

      What makes you use C++ so long?

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

      @@ahmedbugti3152 It's just the language I'm most comfortable with. It's also very powerful, versatile and (next to C) de facto industry standard in many areas of software design.

  • @syntaxed2
    @syntaxed2 Před 6 lety +94

    Any chance you can go more indepth with c++?
    E.g Software patterns, observer/subscriber/design/factory etc, how to structure/organise code, superclasses or not, how to achieve loose coupling etc.
    I feel all of this basic stuff has been done, its all over youtube, the horse is not beaten...its a wet pulp at this point :D Just a hint for future content!

    • @groberti
      @groberti Před 5 lety +19

      I don't think that those topics are C++ specific. It would be nice to have a new Cherno series dedicated to that but those topics would not fit into this C++ course imho

    • @calmsh0t
      @calmsh0t Před 5 lety

      He is right, it is C++ specific since many design patterns cannot be applied directly to c++ since they heavily use concepts introduced in higher level languages

    • @zvxcvxcz
      @zvxcvxcz Před 4 lety +12

      Those aren't things you would usually ask a game programmer. Games are not like many other large projects in this respect, often due to performance targets. Many of these patterns are really geared towards what I will call corporate code... where the assumption is that everyone else using your code is a moron that will ignore comments and abuse absolutely anything that can be abused and in the worst ways possible. Not to say that games don't have any overarching structure, they do. How do I put this though, it's not that they don't have design patterns, but they have different design patterns and they aren't patterns that most proponents of design patterns would approve of.
      Cherno's videos are good and necessary. Tales of the horse's demise are greatly exaggerated. He's pretty much the only person talking about these things on CZcams that can actually convey them to an audience. He's the only one doing C++ videos that makes me feel like I'm back in my university courses listening to my prof rather than to some moron that either doesn't themselves know what they're talking about or with such poor communication skills that the only people that know what they're talking about are people that already knew the subject matter. Go watch some C++ con vids, none of them seem to be able to define lvalues and rvalues in a way that anyone that didn't already know them can understand. Watch Cherno a few minutes and it makes perfect sense.

  • @shavais33
    @shavais33 Před 4 lety +14

    I feel like someone could learn C++ just as well from this video series as they would from a university computer science class. Just as well, if not better. The value here is just too high to consume in good conscience without proffering some level of support. I think I sense a Patreon subscription in my future.

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

      @Over Yonder I'm sorry to hear that your university experience was so terrible. At the time when I attended the university I went to, their computer science and computer engineering undergraduate programs were incredibly intensive.

    • @shavais33
      @shavais33 Před 3 lety +2

      Oh I think you over state the case against going to university a bit. I can appreciate the sentiment, and you might be right when it comes to learning some particular programming language or something, but the engineering pro school I went through took me through big thick advanced textbooks on math, physics, chemistry, things like quantum mechanics and semiconductor physics. Power dynamics, signals and systems, properties of materials.. Things like calculus based discrete math that describe systems for proving (or disproving) the correctness of algorithms.. Electronics and micro electronics.. I built and programmed embedded systems in machine language, assembler, and then C.. advanced concepts in computer architecture.. I designed and simulated the operation of a microprocessor and used equipment whose output could be put into other equipment that would actually manufacture it.. It took me through the advanced details of operating system internals.. All kinds of things. Way too much to even list. I started with 1,000 peers and graduated with 100. Literally 90% washed out! I am probably upwards of the 96th percentile in terms of intelligence, and I found the program to be very challenging. I would never have learned all that stuff on my own, and I don't think I've really found on-line resources that nearly match all that. I still have a small book shelf full of the those big thick advanced textbooks. To me, the online materials I've come across seem more geared toward people with much closer to average intelligence than that of the audience targeted by my university textbooks. Not that you necessarily need all that stuff just to be a programmer, but. I would say that it doesn't hurt. My perception is that the vast majority of programmers I come across who don't have the kind of education background I have don't really think on same the level that I do. Even the ones who I would guess are actually a little smarter than I am. But again, all that said, I do think when it comes to learning about some particular programming language, you might be right that some online resources, like this one, rival or even do better than 1st or 2nd year university courses.

  • @Solo6R
    @Solo6R Před 6 lety +8

    Appreciate the awesome series so far. I still reference your videos when I'm not 100% sure why something works. Anyways.
    REQUEST: Could you do a video on std::string_view vs. std::string, and when to prefer to use one or the other?

  • @work9167
    @work9167 Před 2 lety

    I am happy you make these videos. Because I study Computer Science in my university and it really helps me understand information which I didn't get in my class

  • @gabrielbraz9669
    @gabrielbraz9669 Před rokem

    A example of why would you need to cast something.
    {
    int sum = 27;
    int num = 5;
    double average_1 = sum / num;
    // average_1 = 5
    double average_2 = (double) sum / num;
    // average_2 = 5.4
    }
    Basically, because sum and num are both integers, the c++ will use the integer version of the division operator, and the result will be truncated into an int, before being assigned to the double variable. And in the second case, because of the type casting into a double, the double version of the division is used and the result is expected.

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

    Although I'm making some effort to learn C++ style casts, I also prefer C-Style casts. The only thing that C++ style casts ( a part from dynamic_cast) have useful is improving code readability, but I see them somewhat confusing. Working with C-Style cast is far easier.

  • @redcrafterlppa303
    @redcrafterlppa303 Před rokem +1

    Implicit casts with data loss is one of the greatest sins of c/c++

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

    An important point that I realized looking at my casted variable types, the variables do not actually change to the casted types, only the data is changed. So the variable doubleVar = 5.5, will send 5 when casted as an int, but it still remains a double.

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

    Const cast removes const from memory, this only works with pointers:
    const int* constValue = new int(5);
    int* nc = const_cast(constValue);
    *nc = 7;
    cout

  • @kurt7020
    @kurt7020 Před 2 lety

    "The compiler has looked at that and been like - that's never gonna work." - Sounds like most of my code. XD

  • @RWM_
    @RWM_ Před 3 lety

    static_cast - compiler time conversion check
    dynamic_cast - runtime conversion check

  • @kamran_aghlami
    @kamran_aghlami Před 6 lety +16

    ahhh nice one! I was hoping to see another OpenGL video though. ;)
    keep them coming!

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

      Hey man are you in Cherno discord server? come join us.

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

      Pooria GH I just created my acc on discord, aaaand seems like i’ve been missing this one. 👌

  • @kgothatsontsane3119
    @kgothatsontsane3119 Před 3 lety

    Thank you. You're a legend!

  • @joseponce6250
    @joseponce6250 Před 2 lety

    thanks

  • @buddhasarchive8385
    @buddhasarchive8385 Před 3 lety

    thanks. great video as always

  • @redwing8725
    @redwing8725 Před 4 lety

    Thanks a lot, your videos are always the easiest to understand :)

  • @Lightn0x
    @Lightn0x Před 2 lety +2

    1:05 implicit conversion can have data loss though, can't it? For instance, implicit conversion from double to int truncates to nearest int.

  • @NeoKailthas
    @NeoKailthas Před 3 lety +5

    If I type very strongly, does that make me a good cpp dev?

  • @SankoshSaha_01
    @SankoshSaha_01 Před 4 lety

    any game engine with C++ is gonna give hell with this casting

  • @abhisheksa6635
    @abhisheksa6635 Před rokem

    Thanks

  • @pablo17349
    @pablo17349 Před 2 lety

    Hi Cherno! I am currently studing at 42 school, and your videos are so helpfull. So you know!

  • @exodus8814
    @exodus8814 Před 6 lety +19

    Hello all. I'm having a hard time understanding pointers, I'm okay with everything else in C++, like classes, structs, templates, polymorphism etc..
    It's just pointers that i don't really understand. For example, why do we use const char* message;? Why does it have to be const? Even without const we still cannot change the contents in message.
    Furthermore, why do we have const char* message and char* const message? How can a pointer be constant? Add to that, what is char** argv or argv[]? Malloc, calloc, realloc, free?? Why should I create a variable on the heap? When? Why? What does it give? Why should I free/delete the variables created on the heap?
    I have soo much in mind, if anyone could help, I appreciate it a lot. Thanks

    •  Před 6 lety +7

      I think Cherno made a really good video explaining pointers

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

      The thing you forgot is this is CPP. Ok i will agree that's cringy. For tips I'm a newbie so I'll just recommend you watch all his vids on cpp and others too. Dont take my word for it though.

    • @exodus8814
      @exodus8814 Před 6 lety +7

      Nikola Kostić Thank you very very much for that detailed answer! I appreciate it, sorry if this was too much, I come from high level languages so the concept of pointers and low level stuff is pretty confusing, I tried googling questions and I actually found answers, but still the concept was pretty hard to grasp.
      Thanks a lot for your time :)

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

      I would suggest a book called Effective C++. Its really good

    • @owiley2002
      @owiley2002 Před 6 lety

      The idea behind pointers isn't really a new concept. Think about everyday interactions. Let say that your love one like a grandma wants to give you a present. One way is to visit you directly and give you the present. This is sending you a value directly. But what if I she cannot directly visit you? The next way is sending it via mail or by some address. This is exactly what pointer does. A pointer is a variable which holds data that is the address of something. Your grandma probably has and address book that holds this information and other things like maybe a birthday tracker or planner. The idea is to be able to store a reference to some location becomes key in computing. Some things can not be loaded all at once on a computer, so knowing where it is located becomes very important. The heap vs stack is away to address this problem by having some promises that at least this much information can be seen at once. Later on you will come to see that certain things do not physically exist as limitation but are abstracted ideas that are imposed on the system of computing.

  • @parabalani
    @parabalani Před 3 lety

    I can't believe how accessibly you explained this

  • @leonardomarinovic3492
    @leonardomarinovic3492 Před 6 lety

    So many oportunities to fuck up.

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

    Just a note, but you should prefer '
    ' over endl. Endl also performs a flush which makes it orders of magnitude slower.
    Another small correction, c style casts cannot perform dynamic cast behavior. It can do all the other types though. You should never const cast. Some people use it to add const to variables but a better way is to just use std::as_const

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

    What shortcuts do you use to code that fast? It looks like your using vim instead of visual studio hehe. Would be nice to see a video about that.

  • @darklord3227
    @darklord3227 Před 5 lety

    I have been programming in c++ professionally for a long time. Yet I still enjoy your videos! I mainly watch them for features that I don't normally use. I was wondering if you could create an episode on crash debugging. Thanks!

  • @krisitak
    @krisitak Před 6 lety +9

    Hello Yan. Can you do a video about CMake, makefiles and their alternatives in Visual studio.

  • @yashratnani9877
    @yashratnani9877 Před 3 lety

    thank you

  • @_juanzu_
    @_juanzu_ Před 4 lety

    Thanks ♥️

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

    you make cool videos, keep it up

  • @mattiamarchese6316
    @mattiamarchese6316 Před 2 lety

    At 3:56 you can see Visual Studio transforming that 5.3 in 5.29999999999999

  • @jaywalkingly
    @jaywalkingly Před 4 lety

    thank you for this video

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

    6:45 How is searching for "(int)" not practical?

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

      Because you will have to search for all datatypes like that? In case of C++ style cast you will just need to search for 4 types at max.

  • @darthnegativehunter8659

    8:20 , no actually your explanation was perfect and very understandable. thank you

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

    Could you make a video about memory allocators, like memory arenas?

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

    Any updates on the dynamic casting video? Really interested in this as well as further information on RTTI and reflection in C++. Not something that is clearly and concisely explained online! Thank you for all your helpful videos.

  • @funera3385
    @funera3385 Před 4 lety

    thank you alot
    you helped me alot

  • @catomajorcensor
    @catomajorcensor Před 3 lety

    C++ is not strongly typed, it is very much weakly typed("if (3)"), but it is statically typed.

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

    A very chaotic video.

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

    I learned everything about casting from this video except why we need casting and what does it do. Like what is a static_cast?

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

    What about std::any_cast :D. (It is not an operator, just a function to cast a std::any in the new standard-library features.)

  • @tlhomotsemoteme2423
    @tlhomotsemoteme2423 Před 5 lety

    You are the best!

  • @ikefir
    @ikefir Před 5 lety

    Hi! Though I’m kinda late to the party, let me ask - what is your opinion on “warning: use of old-style cast”? And thanks for the content. Keep it coming!

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

    you're the modern day superhero!

  • @SkSafowan
    @SkSafowan Před 2 lety

    please make another video about how to understand this video !!!

  • @vivekshivhare8820
    @vivekshivhare8820 Před rokem

    Can you make a series on design patterns?

  • @mrboyban
    @mrboyban Před 3 lety

    Cherno you're a trouble mate.! Very a few people can type on a suit, good stuff! thanks

  • @andrez383
    @andrez383 Před rokem

    Cherno, you should have used a black couch for this episode :)

  • @vertigo6982
    @vertigo6982 Před 5 lety +16

    06:42
    Nobody:
    Vim users: "Hold my beer"
    see that.. 2 memes in 1.

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

    Hey, would you make a video explaining object slicing?

  • @pepesilvia3776
    @pepesilvia3776 Před 2 lety

    bruh, that decor is nasty af.

  • @garageman2236
    @garageman2236 Před 3 lety

    Syntax sugar!

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

    Hey Cherno can you do a series on making an interpreter or compiler for a toy language?

  • @AntiWanted
    @AntiWanted Před 3 lety

    Nice

  • @jannesopanen8032
    @jannesopanen8032 Před 3 lety

    Hey do you use laptop's internal mic or do you have external one ?

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

    Could you please add tutorials on websocket? Create websocket services using C++, Websocket client, etc.

  • @dominikvereno8404
    @dominikvereno8404 Před 6 lety

    Would be awesome if you could make a video about regular expressions!

  • @sumitparwal4086
    @sumitparwal4086 Před 3 lety

    can you explain about inheritance in nested class as in can the classes inherited by enclosed class will also be inherited by nested class if not how can we include them

  • @footballCartoon91
    @footballCartoon91 Před 3 lety

    Man
    I need your help,
    I want to cast an int variable of an array into string or bool or maybe a constant number.
    i.e
    int *arrayPointer=new int[ 5 ];
    Then i want to just represent the *arrayPointer in smaller byte size,
    Because later I would like to check whether the user input insert the correct number in the correct order.
    This can be used to create a new programming language!
    Thanks

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

    the reinterpret_cast at 7:50 doesn't work for me. other than that, great video :D

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

    Hello, would you make a video about exception and exception handling?

  • @GenjaOrigins
    @GenjaOrigins Před 3 lety

    Videos of HashMap HashSet etc please

  • @tianhepeng9162
    @tianhepeng9162 Před 6 lety

    Where is the in-depth dynamic_cast video? I find information from cppreference is so hard to understand. Hope the dynamic_cast video would come ~

  • @pnuema1.618
    @pnuema1.618 Před rokem

    The word "static" always refers to "compile time"

  • @bloodwolf8307
    @bloodwolf8307 Před 4 lety

    cool

  • @connorhorman
    @connorhorman Před 6 lety

    I think that not everything can be done with a c-style cast. If x is an int or an int&, I can static_cast it to int&&, but do (int&&)x. (can't convert an lvalue expression to an xvalue, but static_cast can transform types from an lvalue to an xvalue. (Though not directly to a prvalue.)

  • @michael541
    @michael541 Před rokem

    Do you know what native advertising is?))

  • @sirfalcon1
    @sirfalcon1 Před 6 lety

    Do you have a video on Try Catch error handling?

  • @obinator9065
    @obinator9065 Před 6 lety

    Can you do a video on iterators?

  • @Beatsbasteln
    @Beatsbasteln Před 4 lety +3

    did anyone notice that he is at different places in almost every video? how does he even do that?

  • @cavidanbagiri1884
    @cavidanbagiri1884 Před 5 lety

    hi Cherno. why dont you create c++coursein udemy. please create course and speak about c++ and mysql connection and some function. Thank you very much

  • @crait
    @crait Před rokem

    Yeah, you can search for casting without static_cast if you use the constructor-style casting.

  • @samkenneth6251
    @samkenneth6251 Před 6 lety

    Are you going to do more Sparky episodes??

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

    Make video about dynamic cast asap!

  • @dockstaal7110
    @dockstaal7110 Před 6 lety

    lol I was just learning about this for my exams and now you upload a video about it.

  • @gabrielbarrantes6946
    @gabrielbarrantes6946 Před 10 měsíci

    Is casting a good practice? I feel is so dirty that should only be done if you are looking to implement something not readable but quite efficient like que quake algorithm lol

  • @osere6432
    @osere6432 Před 5 lety

    Bit fields please?

  • @ridaennasry2558
    @ridaennasry2558 Před 2 lety

    "C-style cast can achieve of all that so this isn't really adding new functionality i's kind of syntax sugar to your code" that's absolutely a wrong statement, C++ casts add many functionalities, for example static_cast is more restrictive and does compile-time checking for valid casts hence you can't cast pointers which point to different data types in c++ static_cast, while in c-style you can, you can't too take the constness off a type using c++ static_cast while in c you can, and for other c++ casts they add their functionality too, do you readings !!!!!!!!

  • @michaltch9246
    @michaltch9246 Před 6 lety

    Can u please make video about filling code with module tests? Using boost c++ libraries.

  • @fidanrli7456
    @fidanrli7456 Před 6 lety

    Hi could you explain inline function ?
    and storage class?
    These are my exam questions .

  • @dms2472
    @dms2472 Před 6 lety

    Next , videos about shortcat typing for more productive coding pliss

  • @valizeth4073
    @valizeth4073 Před 6 lety

    Could you answer why you need to typecast the C function malloc?
    like
    char* ptr;
    ptr = (char*)malloc(size * sizeof(char)); ??

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

      I guess because malloc returns a (void *) , also malloc does not know for which type we are allocating memory for so we have to cast it back ( size is provided using sizeof(type) , in your case its sizeof(char))

    • @valizeth4073
      @valizeth4073 Před 6 lety

      I know how you use malloc to allocate sizes and so on, just didn't get the casting.

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

      +Vali Zeth - C++ has more type safety than "C". "C" doesn't care what type you assign a void pointer (void*) to, so you could assign a void pointer to anything without it caring, but C++'s extra type safety needs you to explicitly tell the compiler what type you want the void pointer casted to, because a void pointer is a pointer to anything (unknown type)...

    • @valizeth4073
      @valizeth4073 Před 6 lety

      One thing I still haven't gotten though is why pointers are of type void. A pointer returns a memory address of an object / variable. The address is still a value though, void = emptiness. So how can it return an adress and yet nothing at the same time?

    • @joshs2475
      @joshs2475 Před 6 lety

      +Vali Zeth - A void pointer is just a RAW Address.
      It is basically like saying, that whatever receives this pointer can treat the memory space at that address as any type it would like.
      So if I cast a void* to a char* ("char* ptr = (char*)malloc(10)"), I'm just telling the compiler that i want to either read or write to this address as the value type of "char" (1 byte data type).
      I could also say I can cast the void* to a int* ("int* ptr = (int*)malloc(10)"), I'm telling the compiler that I want to either read or write to this address as a value type "int" ( 4 byte data type)
      So as you can see, i can treat a void* as anything I want due to the fact that the compiler doesn't actually know what is at that address because it is a pointer to an "Unknown data type" (void*)...

  • @zikdoz
    @zikdoz Před 6 lety

    Isn't it better to check for nullptr instead of regular 0 (or NULL macro) with dynamic casts?

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

      zikdoz
      Quick answer: no
      Long answer: Nullptr means, just that, a null pointer. So checking a pointer with nullptr makes sense.
      Technical answer: it doesn't matter, but you'll lose friends.

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

      itsArtem
      The first example shows what's going on in memory, the second shows what you're trying to do.

  • @Xgamesvidoes
    @Xgamesvidoes Před 4 lety

    Thanks! :D

  • @sbn0671
    @sbn0671 Před 6 lety

    Hi guys! I have trouble with glsl shading language. I use #version 120 because of my hardware. I've tried inverse function for lighting but i can't use it. It needs minimum #version 150. Is there other option than using inverse function?

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

    Amazin.

  • @bextract0
    @bextract0 Před 6 lety

    yeeeeeeeeeeeeeeeeeeeeeeeeeet

  • @joeydicastro
    @joeydicastro Před 6 lety

    Guys, do you know about a C++ developers group? on any social network, I´m really interested on become part of one since I enjoy C++ a lot, and I want to learn from others and also share knowledge.

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

    Casting is one of the most dangerous things you can do in C++ programming, should be avoided except in a few cases, and is a sign of poor programming technique. The third quickest way to blow up your program is with a bad cast (1 is divide by zero, 2 is deref null pointer). The acceptable cases are when doing simple numeric conversions, like in this video, and when you want to use void pointer arguments. Most likely when searching for code you will do it based on variable names or strings, not language keywords.

  • @GohersWay
    @GohersWay Před 4 lety

    7:47