Move Semantics in C++

Sdílet
Vložit
  • čas přidán 9. 06. 2024
  • The first 1000 people who click the link in the description will get 2 free months of Skillshare Premium: skl.sh/thechernoproject8
    Patreon ► / thecherno
    Instagram ► / thecherno
    Twitter ► / thecherno
    Discord ► thecherno.com/discord
    Series Playlist ► thecherno.com/cpp
    This video is sponsored by Skillshare.

Komentáře • 444

  • @TheCherno
    @TheCherno  Před 4 lety +232

    Hope you all enjoyed the video! This is just the beginning of the Move Semantics Saga™, still to come is std::move, the move assignment operator, and more! Thanks for watching! ❤️
    P.S. RELAX GUYS THE PS5 PART 2 VIDEO IS COMING TOMORROW

    • @nothingtosee226
      @nothingtosee226 Před 4 lety

      Do you have your own personal library of functions, classes, namespaces, etc? My professor told me that it's important for programmers to have their own tools which make their own methods easier.

    • @AtlasPrimeV
      @AtlasPrimeV Před 4 lety

      ok

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

      Excuse me, I know this is out of the point, but, do you know a way to turn off auto save in Visual Studio Community 2019?

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

      Thanks for starting with Move Semantics.. looking forward for more about this..👍

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

      Can you make a video about the rule of 3 and the rule of 5 and how to implement them properly?

  • @shah.kairav
    @shah.kairav Před 2 lety +118

    In case anyone is wondering why there is only one "Destroyed" line being printed on Cherno's terminal, remember that his program halts due to "std::cin.get()". Once he presses Enter, he should see the other "Destroyed" message.
    Reason for two destroys: 1 where the hollow object is destroyed + 1 where the actual heap memory is deallocated.
    Hope this saves time for someone and helps!

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

      Thanks, I was getting two "Destroyed" and was like what the heck ;). Then I debugged it and the first one is destroying object with size of 0 and data as null, so I actually deduced this somehow myself too...

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

      Thanks! I was wondering exactly that. Saved me some googling

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

      yeah I was wondering! saved me some time!

    • @alecmather
      @alecmather Před rokem +1

      Bless your soul...

    • @Mmmaris_Chan
      @Mmmaris_Chan Před rokem

      it helps! should read the comment first before figuring it out myself🤣

  • @alextiga8166
    @alextiga8166 Před 4 lety +90

    This C++ series will never run out of topics to make a video about :)
    I've got too many requests what to cover in next videos.
    For example Multithreading: it would take forever to make videos about threads, locks, lock-free synchronization, fibers, coroutines etc.

    • @godnyx117
      @godnyx117 Před 3 lety +8

      This didn't aged well...

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

      ​@@godnyx117 Oh yeah I expected this series to cover more advanced topics but had to do my own research on the topic instead.
      Still a very great series and the best C++ series there is on CZcams!

  • @Norhther
    @Norhther Před 3 lety +73

    8:24 noexcept is important for performance reasons. In a talk I saw, the example presented was 60% faster using noexcept because of the nature of push_back operation in std::vector. So keep that in mind!

    • @IndyR0ck
      @IndyR0ck Před rokem +1

      what's the talk you saw? :)

    • @torstein5
      @torstein5 Před rokem +7

      @@IndyR0ck Probably this one: Back to Basics: Move Semantics (part 1 of 2) - Klaus Iglberger - CppCon 2019

    • @IndyR0ck
      @IndyR0ck Před rokem +3

      @@torstein5 thx!

  • @mickaelfleurus9944
    @mickaelfleurus9944 Před 4 lety +8

    I owe you a huge thank you ! I've been trying to understand what are lvalue and rvalue for quite a long time, and an even longer time for what the move semantic was all about, and I've finally understand it with your video.
    I'm in a big learning phase right now, and your channel is an awesome way to improve myself. Thanks for the good work !

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

    Thank you for this video! This is one of my favorite series on CZcams and this is a topic that can be quite confusing but you simplified it beautifully. Great job and keep up the good work

  • @toddwasson3355
    @toddwasson3355 Před rokem +6

    Thank you so much for this move semantic vid. This has been wildly helpful to me today. Love your channel, your C++ playlist is by far my favorite of all C++ content.

  • @iiTzLamar
    @iiTzLamar Před 4 lety +350

    m_Data = new char[m_Size] should be freed with delete [ ] m_Data; and not delete m_Data;

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

      It should also be m_Size + 1 when allocating for the null terminator

    • @Spirrwell
      @Spirrwell Před 4 lety +51

      @@gideonunger7284 Not necessarily. You don't need to store a null terminator if you know your string's size. Though it can be useful with C style string functions.
      But he should've used delete[] or maybe even allocated with a unique_ptr instead.

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

      That is good advice in general, however in this case is not necessary because the destructor of char is a no-operation. In this specific scenario is not necessary to call the destructor of every object in the array by calling delete[].

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

      @@Spirrwell yes sure if you don't work with any c string functions or pass that pointer to the outside it's fine. But it's still dangerous in c++ since the assumption of a char* is that it's null terminated. Rust doesn't use null termination but there it's the standard of the language so it's not a problem anywhere

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

      @@gideonunger7284 It's already dangerous to assume the const char* passed to strlen() will be null terminated. In C++ a compiler might add a null character to such values in double quotes but a C compiler won't do that implicitly which makes sense because you will know the length of a constant array of chars in your code already as developer.

  • @ericprincen3345
    @ericprincen3345 Před 2 lety +15

    My first professional language was C++, and I moved from it in 1998 when I started working in Internet / Social media. I've moved back to writing low level code on hardware again, and I've been enjoying your videos a great deal. C++ has come a long way, and you've made my transition back a very easy one.
    BTW, I love the Hazel stuff too. Last time I worked with 3D graphics was when bsp trees were a new thing (and "fingering John Carmack" was something that was regularly done...) Fun to see how far that has all gone. I'm not doing any graphics right now, but fascinated with amount of forward movement in the field over the past 25 years.

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

      Pointing fingers at sir John Carmack? Sounds like the DOS days to me!
      I'm sorry I'm asking this _TWO_ years later, but in your opinion, how fast is C++ at evolving?
      It is often called an ever-evolving language, and especially with us getting a new standard every third year, I, a beginner, feels the same way.
      Thoughts?
      I invite other veterans to answer, too!

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

    Holy shit. I thought I knew things. I don’t know anything. This channel has been such a blessing. Instant sub.

  • @JayAnAm
    @JayAnAm Před 4 lety +114

    Wow, naming a parameter "string" is quite... courageous:-)

    • @ianpan0102
      @ianpan0102 Před 3 lety +24

      Unless you're using namespace std, it doesn't really matter.

    • @unsafecast3636
      @unsafecast3636 Před 3 lety +24

      @Artem Katerynych
      #include
      using namespace std;
      string string(string string) {
      string string = string;
      }
      int main() {
      string string = “string”;
      string(string);
      }

    • @barmetler
      @barmetler Před 3 lety +23

      @@ianpan0102 Using namespace std is a cardinal sin anyway

    • @alphazero4587
      @alphazero4587 Před 3 lety +15

      @@unsafecast3636 Bruh Bruh(Bruh);

    • @user-si9jy3zs1j
      @user-si9jy3zs1j Před 2 lety +2

      @@unsafecast3636 god

  • @khoing1111
    @khoing1111 Před 4 lety

    Founding this channel is a god damn blessing for me. Why did I not know about you earlier? Probably spent my whole life of luck on this.

  • @abdullahamrsobh
    @abdullahamrsobh Před 4 lety

    That video really came, when i needed it thanks Cherno

  • @BillThaPill
    @BillThaPill Před 4 lety

    Need that part 2 reaction video bruh. U be preaching facts and I be feeling that. Keep up the good work my guy. Just run that reaction video for ya boy. You’re killing me Smalls

  • @user-ez2ze8he9u
    @user-ez2ze8he9u Před 4 lety +63

    4:40 should be delete [ ]

    • @TernaryM01
      @TernaryM01 Před 4 lety +8

      He should've tested it on PVS-Studio to see if it can detect the memory leak.

    • @abdallahrashed1947
      @abdallahrashed1947 Před 4 lety

      what is the difference ?

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

      @@abdallahrashed1947 "delete" destroys a dynamically allocated object and "delete[]" destroys a dynamically allocated array

    • @jinchengzhang25
      @jinchengzhang25 Před 3 lety

      @@TernaryM01 underrated comment lol

  • @Sala-lh9fu
    @Sala-lh9fu Před 3 lety

    Thank you so much fir being so thorough with your explanations! :)

  • @hamdaman3593
    @hamdaman3593 Před 4 lety +25

    Yes the ps5 part 2 is coming this weekend, whose looking forward to that

  • @CacheTaFace
    @CacheTaFace Před 4 lety

    Great explanation! BTW your hair looks great here

  • @Albert-lr7ky
    @Albert-lr7ky Před rokem +12

    Very nice and excellent video!!! Tho I've got a small question: should we be usong "delete [ ] m_Data" in the destructor? Since it is created from "new [ ]"

    • @jamesbaguio2386
      @jamesbaguio2386 Před rokem +1

      Yes, the program needs to clean the continuous block of memory allocated to m_Data.

  • @chawnneal159
    @chawnneal159 Před 3 lety

    OMG! Ever since your view_string video, I've been obsessed with making my own string_view! After this video I was able to mimic some functionality via referencing the same memory! With some extra steps I can prevent writing! Thanks so much!!

  • @DassVeryGood
    @DassVeryGood Před rokem

    This makes way more sense than what was being taught to me, where we just pushed multiple of the same object into a vector with different values (i.e. vec.push_back(Move{10})... vec.push_back(Move{n})). Sure it works, but doesn't help with visualizing the need to use a move constructor or semantics. This video helps so much, it just clicked instantly after watching this!

  • @nallaprakash6901
    @nallaprakash6901 Před 4 lety +8

    Man I was waiting for this from ages 😭😭😭😭, FINALLY Thanks Chernikov 😄

  • @lmnefg121
    @lmnefg121 Před 3 lety

    really love your videos and i have learn a lot things from them

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

    I learned so much about it but still dont know how to use it. So happy to see that you do a video on it as you always go very deep into these topics.

  • @Ximaz-
    @Ximaz- Před 4 měsíci

    I really enjoyed your video, thanks.

  • @poggly
    @poggly Před 3 lety

    This helped a lot, thank you!

  • @mikewajda9912
    @mikewajda9912 Před 4 lety

    Just found you channel and watched almost all of your c++ videos and some of your game engine series. Awesome job and thank you! Wanted to ask how you feel about rust compared to c++ and if you have had any experience with rust at all?

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

    Yesss, I've been waiting for this, THANK YOU :)
    Will you make a video on Return Value Optimization in the future? I don't really get that topic, my professors aren't good at explaining, but you are

  • @grownupgaming
    @grownupgaming Před 2 lety

    Great video, very clear!

  • @cole-nyc
    @cole-nyc Před 4 lety +85

    Great video! one comment though: When you heap-allocate an array you have to free it with 'delete[]' instead of 'delete'. Your code frees only the first element of the array.

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

      Technically its UB with char* string literals

    • @charoniv5631
      @charoniv5631 Před 2 lety

      what really?

    • @tolkienfan1972
      @tolkienfan1972 Před rokem +1

      According to the standard, using delete instead of delete[] is UB. In practice, both delete and delete[] are simply forwarded to the libc function "free", and therefore end up behaving the same. Of course I an NOT advocating using delete instead od delete[], that would be terrible practice. But there isn't an implementation out there that only frees the first element. In fact there isn't a function you could use to only free the first element. Even realloc won't do that.

    • @Flinsch77
      @Flinsch77 Před rokem +1

      With `delete` instead of `delete[]`, the whole array gets freed, no problem so far, but only one destructor might be called: that of the first element. This doesn't matter in the case of a `char` array, but it can be relevant for more complex objects, which in turn might allocate their own memory (which would then have to be freed again).

    • @tolkienfan1972
      @tolkienfan1972 Před rokem +1

      @@Flinsch77 that is correct. Thanks for that

  • @AugusteeeJoJo
    @AugusteeeJoJo Před 3 lety

    This is amazing! Thanks!

  • @tannishkmankar3998
    @tannishkmankar3998 Před rokem

    Great you solved one headache for me, thanks

  • @TarunKumarSaraswat
    @TarunKumarSaraswat Před 3 lety

    Wow❤️ simplified to the core

  • @filosofisto
    @filosofisto Před 4 lety

    Excelent explanation, thank you

  • @B4dD0GGy
    @B4dD0GGy Před 4 lety

    entertaining while *learning*, love it

  • @MKolbe-jh6yh
    @MKolbe-jh6yh Před 3 lety

    THANK YOU MY FRIEND! YOU JUST SAVED MY SEMESTER :D

  • @AllothTian
    @AllothTian Před 4 lety

    It's worth noting that we need to make the moved-from string "hollow" because its original constructor will still fire. In essence, a moved-from object can have multiple instances of its destructor being fired. This, as you can imagine, is rather inefficient. The alternative is what's called a "destructive move," but the design committee of the language couldn't figure out a way to make that fit with the existing object model. For the curious, you can look up Howard Hinnant's and Sean Parent's posts on this matter.

  • @user-nx5ob7ny4l
    @user-nx5ob7ny4l Před 2 lety

    u give a human touch to a very dry and difficult topic , very helpfull

  • @jh-lp7cg
    @jh-lp7cg Před 4 lety +1

    Also, I believe any time the compiler can use Return Value Optimization do not try to use move semantics for return values. RVO will be more efficient.

  • @JJFoxLegacy
    @JJFoxLegacy Před 4 lety +7

    Dude loved the ps5 video, can't wait for the next part !!!

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

    Thank you so much Cherno!! You're seriously the best 😊

  • @ckacquah
    @ckacquah Před 4 lety

    Your videos always explain everything better 👍...
    Nice example

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

    Hey, Cherno, the video after Static Analysis in C++ is now marked as private. Was that intentional? Or was it also on move semantics but now you're redoing it and thus removed the old one? Loving your content!

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

    You can do printf("%.*s
    ", (int)m_Size, m_Data); rather than that printf loop - it's easier, more efficient, and won't b0rk when you call it from multiple threads since the single call will happen under the stdout lock.

  • @tannishkmankar3998
    @tannishkmankar3998 Před rokem

    Still working as of today, ty!

  • @Nick-tv5pu
    @Nick-tv5pu Před 9 měsíci

    Great video, I always look forward to your stuff.
    That said, at 0:53 isn't that what references (pointers) are for? You don't have to copy a value to/from a function if you just pass/receive a pointer?

  • @halaszka2949
    @halaszka2949 Před 2 lety

    I KISS YOUR EYES! I didnt unterstand it with my book, but then i found you. R Value Refenrences and Move Semantics are very usefull

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

    The way you added cast before bringing in std::move was really the way std::move should always be explained.

  • @shubham91219
    @shubham91219 Před 4 lety +5

    Hey Cherno, could you please make some videos on lock free programming and memory ordering as well? Thanks for all the great content so far.

    • @KishoreG2396
      @KishoreG2396 Před 4 lety

      Memory ordering is a very tricky subject.

  • @Toccobass13
    @Toccobass13 Před 3 lety

    Thank you!

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

    Hello Cherno, please keep uploading new features of C++ 11/14/17/20.
    Your content is great.

  • @DIYGuy85
    @DIYGuy85 Před 4 lety

    Can someone please give this guy an award!

  • @Idlecodex
    @Idlecodex Před 4 lety

    Man... God bless you! :)

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

    You missed pointing out that if you have a true temporary it will also use the move constructor without needing ::std::move

  • @yohan7027
    @yohan7027 Před 4 lety

    Should you use move with functions?
    Let's say:
    std::vector add( std::vector a, std::vector b) { ... }
    and I need to use whatever is passed into a and b later, is it better to pass by reference or move? I know that anything moved shouldn't be used again, so is there a way to move back after the function is done? Like making the parameter a const move or something?
    I ran some trials and move was faster than reference (is that true?) but it's pointless if it leads to undefined behavior.

  • @267praveen
    @267praveen Před 4 lety +2

    Finally ... It's here. Thanks Cherno.
    Next awaited .....
    Regex
    SFINAE
    Random engines

  • @rafalmichalski4893
    @rafalmichalski4893 Před 4 lety

    Less typing with "puts" (it adds '
    "), so no need to use printf only to print non-formatted string with "
    " at the end. Anyway great material Cherno.

  • @ivanpolyakov5746
    @ivanpolyakov5746 Před 4 lety

    Excellent example with string!

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

    This is freaking greatest explanations ever for move sementic with demo : this explains whats going on underneath actually!! And how move sementic helpful in performance

  • @HamidMehr
    @HamidMehr Před 2 lety

    So after attempting to learn move semantics/rvalue references for n times ( n >= 100) this finally clicked. Thanks, Cherno!

  • @shefman200
    @shefman200 Před 4 lety

    Ayo my g we respect your regular content...but we want that that ps5 part 2

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

    These videos are here to stay to help people even 5+ years from now 😨

  • @Yupppi
    @Yupppi Před 7 měsíci

    Some of the videos are really clear cut and understandable, but I must say that in this one I dropped the ball very quickly. In like 10 seconds there appeared 50 lines of code and multiple class with their methods, privates, publics, printf, memcpy, delete etc.

  • @J-PSX
    @J-PSX Před 4 lety +25

    The PS5 part 2 is coming this weekend

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

    Top class teacher for Cpp

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

    Literally just watched your lval rval video an hour ago. Good timing :)

  • @Swonkasaur
    @Swonkasaur Před 4 lety

    Thank you for the free 2 months of skill share ;D

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

    Move semantics is basically the same thing as what Patrick said from sponge-bob. Why don't we just move the bikini bottom. In other words, moving a large object without copying it. It would be a real pain to build a town, rebuild it somewhere else, and tear the old one down.
    Thanks btw, your videos semantics and R/L values really helped me out a lot. Would have probably given up on my textbook if I hadn't seen this video.
    The moment I realized how R and L values worked was when you mentioned the phrase "Location Value". All the rules suddenly clicked. I realized L values were like vacant houses for variables. When you assign 10 to x, you are assigning the value of 10 (an R value) to a memory location (l value). Reference variables allow you to sort of bend the rules a bit...

  • @xxx489Rockstar984xxx
    @xxx489Rockstar984xxx Před 4 lety

    He finally did it

  • @meirklemfner114
    @meirklemfner114 Před 4 lety

    My goodness! I didn't know the last part...

  • @valizeth4073
    @valizeth4073 Před 4 lety

    What made you switch to printf over streams? Personally I always use fmtlib, and I can see that printf is quite similar, but why tho?

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

    I don't think I've ever been so excited to watch a C++ video before xD. They should put you on Netflix Cherno! ;)

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

    Do all containers in modern C++ have move constructors and move assignment operators by default? and if so how do you use them?

  • @mkhadka123
    @mkhadka123 Před rokem

    Ok my head is spinning now

  • @RaonCreate
    @RaonCreate Před 3 lety

    well done explained

  • @bipingosain6870
    @bipingosain6870 Před 4 lety

    Greate Video! Can you please do a series or at least a video to give an overview of how to get started with Authoritative Multiplayer Code with C++?

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

    4:27 😲 he used printf( ); i cant believe it.

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

    Thanks for the great explanation. I just have a note, you are moving the curser a lot making it a bit difficult to follow. cheers

  • @sumitgupta7553
    @sumitgupta7553 Před 8 měsíci

    Hi Cherno, you are just amazing.. Well described. But can you help me in finding out where I'm getting wrong, when I say we can do the work, you done in move constructor, in the copy constructor even in that constructor we only getting reference of the object, then what is the need to define separately!

  • @alexandrumarcel3696
    @alexandrumarcel3696 Před rokem +1

    if we use the new[] operator for allocating memory for m_Data, shouldn't we use the delete[] operator in the Destructor, or am I missing something ?

  • @erenenadream
    @erenenadream Před rokem

    Hey I want to ask a question about copy constructor that you have coded. In an example like this:
    String first("The first string");
    String second("The second string");
    first = second;
    Will the code above cause memory leak because of the copy constructor that you have written? The copy constructor allocates new memory all the time but it doesn't delete the old one if it's exist. @TheCherno

  • @activex7327
    @activex7327 Před rokem +1

    You should explain the explicit std::move() call in entity ctor. If you look at the ctor of entity it already accepts rvalue so why explicit move if the signatures of entity ctor and string ctor match. Why is function overloading not working here all the way down?

  • @john_codes
    @john_codes Před 4 lety +30

    Everyone: RELEASE PT 2 OF THE PS5 REACTION VIDEO
    Cherno: Here's a c++ move semantics video.
    Lol just kidding. I know he's releasing the video on Saturday. Can't wait!

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

      John True Statement,this popped up and I was like where PS5 part 2 video everyone is waiting for😁

    • @redscorpion9325
      @redscorpion9325 Před 4 lety

      How do you know its being released on Saturday?

    • @Optamizm
      @Optamizm Před 4 lety

      @M. de k., could be the Discord?

    • @john_codes
      @john_codes Před 4 lety

      @@redscorpion9325 go to his channel and look at his community post. He said it's coming on Saturday:)

  • @alecmather
    @alecmather Před rokem +2

    One thing I ran into while going through this video (apologies for my novice) is that you're using this shorthand syntax to assign the Entity->m_Name property in the constructor by doing ": m_Name(name)". Out of curiosity, I tried doing it the way I'm familiar with (just a normal "m_Name = name") inside the body of the Entity constructor, and this caused an error in the destructor of the String class. No idea why/how these two property assignments are different, probably worth an explanation somewhere? Thanks again for making awesome videos!

    • @alexandrumarcel3696
      @alexandrumarcel3696 Před rokem +1

      the difference between ":m_Name(name)" outside of the actual Constructor body and "m_Name = name" is that the first one is calling the Copy Constructor and the second one is calling the Operator = of the String class. Since there is no overloaded version of the operator = the compiler uses the default version provided by the compiler, but as the compiler doesn't know how you want to treat your pointer variable (m_Data) it just simply doing a shallow copy. So "m_Name = name" is basically assigning the pointer name.m_Data to m_Name.m_Data (m_Name.m_Data = name.m_Data), which will result in both name and m_Name pointing to the same address. So when "name" goes out of scope and the Destructor gets called, this will result in "m_Name.m_Data" being a dangling pointer, so when the m_Name object's Destructor gets called it will try to delete the pointer m_Name.m_Data which has been previously deleted when "name" got out of scope. Or at least this is I think happened :D Try to overload the operator = if you want to use the "m_Name = name" version

    • @alecmather
      @alecmather Před rokem

      @@alexandrumarcel3696 you're a G for this explanation lol I also found that he later does a whole video on this difference which also helped a lot, thank you!!

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

    Many years ago when I learned K&R C, ignoring the NULL sentinel was one of the most painful programming lessons I learned. Bad things can happen if you don't allocate room for the NULL sentinel and then use C standard library string functions.

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

    Cherno, you could perfectly voice Mike Judge, several B&B characters including Butthead. Did anyone point that out? Quite good resemblance in this video!

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

    At 5:15, can you confirm, do we need the copy constructor because the default `String` constructor will only copy the `m_Data` pointer, but not its contents (as you mentioned in your Copy Constructor video)? I was a little confused at first because `String m_Name` in `Entity` is not a pointer.

  • @MrSebaleme
    @MrSebaleme Před 2 lety

    When you are running your code at 11:40, the compiler still choose the copy constructor over the move constructor. Would it work if you explicitly forbid your copy constructor to accept rvalues, by making the input parameter not const (line 15)?

  • @MadpolygonDEV
    @MadpolygonDEV Před rokem

    im confused about following, when setting other.size =0 arent we also setting our copied data to 0 since we re pointing to the same value? Am I correct to assume that when we use the && function we should think about the variables as pointers instead of references in a sense that were purely assigning pointers now and arent changing the values directly, only the addresses.

  • @mgancarzjr
    @mgancarzjr Před 4 lety

    Fairly certain this conundrum was a chapter in Effective C++.

  • @norbertnemesh
    @norbertnemesh Před 4 lety

    You should tell people about swap idiom in this Move Semantics Saga™

  • @StrangeIndeed
    @StrangeIndeed Před 4 lety

    Good stuff

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

    I'm new to coding/programming. I started with JS and PHP. Along the way I learn OOP, and design patterns. Accidentally, I learn to understand Java and C++ syntax thanks to CZcams video suggestions like this. Thank you!😊

  • @student99bg
    @student99bg Před rokem

    This has finally clicked. It clicked only after I realized that copy constructors, move constructors etc. are just regular constructors which take in a const reference and an rvalue reference, respectively.
    My knowledge of Rust got in the way of understanding this at first.
    So, basically, if you want a constructor to take ownership of the existing data instead of copying it, the constructor should accept rvalue references.
    What's weird is that C++ seems to be different than Rust here.
    Rust's compiler doesn't allow you to use moved objects. As far as I understand in C++ you can go on and use objects even after casting them to rvalue reference and passing them to a constructor. Compiler will let you do it.

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

    any chance of a map/unordered_map, with hashing/compare function creation for custom key types tutorial?

  • @powerslideita
    @powerslideita Před 4 lety +43

    When's part2 of the ps5 event?

    • @J-PSX
      @J-PSX Před 4 lety +8

      This week end.

  • @ZhangYingtiden
    @ZhangYingtiden Před 4 lety

    Sorry if my question is too obvious... still learning the basics.
    What happens if in the move constructor, you cannot mutate the m_Data field of your temporary rvalue object?
    For example, if the String class declares the m_Data field as "const char *const m_Data", then you wouldn't be allowed to assign nullptr to the rvalue object. Should we just avoid doing (sometype *const) in general in C++? Or is there a workaround for this?

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

    5:35 Shouldn't we also delete[] m_data for this String, before we put the other String's data on it ?
    EDIT: I am sorry. At construction time the this String obviously can't have any data that we need to delete before assigning other's data.

  • @philpesce
    @philpesce Před 2 lety

    I followed along and at the 11:50 mark, where you change the String&& cast to using std::move, the result is the copy constructor returns to being used. Any ideas why this could be?

  • @LangDau
    @LangDau Před 4 lety

    Good stealth