std::move and the Move Assignment Operator in C++

The first 1000 people who click the link in the description will get 2 free months of Skillshare Premium: skl.sh/thechernoproject9
Patreon ► / thecherno
Instagram ► / thecherno
Twitter ► / thecherno
Discord ► thecherno.com/discord
Series Playlist ► thecherno.com/cpp
This video is sponsored by Skillshare.

Пікірлер: 329

  • @MrSuperdude6
    @MrSuperdude63 жыл бұрын

    I really wish I had've just watched your entire C++ series instead of listening in any of my lectures. This has been miles more helpful and easy to understand than any programming class I've had.

  • @Dante3085
    @Dante30853 жыл бұрын

    A good ways to understand std::move() is that you are basically saying "I don't need the object that I am passing anymore. I am allowing someone to steal from it."

  • @supremedeity9003

    @supremedeity9003

    3 жыл бұрын

    Voluntarily letting your stuff be stolen.

  • @glee21012

    @glee21012

    3 жыл бұрын

    It basically steals the pointers, or takes ownership of them, very powerful, like Cherno says "no new allocation". I can only think of using it when I have a temp variable.

  • @Evan490BC

    @Evan490BC

    2 жыл бұрын

    @@glee21012 The fundamental Computer Science concept here has to do with Type Theory, and in particular linear (more correctly, affine) types: any object can only be owned by a single owner at any given time. Because C++ is the way it is (i.e. a mess) it's harder to enforce. But it (i.e. C++) is a necessary evil.

  • @anon1963

    @anon1963

    8 ай бұрын

    @@Evan490BC c++ is only a mess when your bad code makes it seem like a mess.

  • @lucascoef
    @lucascoef Жыл бұрын

    I've just aced a C++ technical interview thanks to this video. Your ability to explain is unparalleled. Keep doing the good work!

  • @svenbtb

    @svenbtb

    11 ай бұрын

    i know this is 7 months old lol but congrats! Hope the job is going well and I hope to one day soon ace a C++ interview myself using what i learn from Cherno

  • @boondocksripoff2237
    @boondocksripoff22373 жыл бұрын

    I now realize I still dont know the basics of c++

  • @1vader

    @1vader

    3 жыл бұрын

    I thought the same thing after watching the video but honestly, it more so made me realize what a crazy and confusing language C++ is. Stuff like this is why I prefer C or Rust.

  • @jyl9063

    @jyl9063

    3 жыл бұрын

    Move constructors/assignment is basic C++

  • @PixelThorn

    @PixelThorn

    3 жыл бұрын

    @@1vader or C#, or Python...

  • @Dante3085

    @Dante3085

    3 жыл бұрын

    @@1vader That's not even the difficult part of the story of move semantics. Research perfect forwarding and reference collapsing

  • @Stefan-tw3fo

    @Stefan-tw3fo

    3 жыл бұрын

    @@1vader Same here.

  • @justinjames3028
    @justinjames3028 Жыл бұрын

    I did not understand std::move() until I watched this. To give some background, I was first exposed to C++ in 1994 but moved away from it before r-value references were a thing. I like the examples he provides. The problem I have with most explanations is that they use words like "tells the compiler it can cannibalize the object" or some such nonsense. That leaves the impression that the compiler is doing some type of cannibalizing or other magic behind the scenes. Saying it just turns the object to an r-value reference and as a side effect can cause the move constructor or move assignment operator to be called. The person cannibalizing is not the compiler, it is the implementor of those operations. After seeing this I wrote a simple class like he had and played around with it. It is not nearly as complicated as I thought.

  • @ianpan0102
    @ianpan01023 жыл бұрын

    Incredibly helpful and enjoyable video -- thousand thanks Cherno!

  • @mikeweathers5726
    @mikeweathers57263 жыл бұрын

    Great video! This is my favorite C++ series. I hope you make one on perfect fowarding soon.

  • @johnnyserup5500
    @johnnyserup55002 жыл бұрын

    you definitely made a cool explanation of the std::move functionality - thanks, this is going to help many developers I think

  • @MarcosVinicius-bd6bi
    @MarcosVinicius-bd6bi Жыл бұрын

    Dude, you're amazing. Your videos are helping me a lot!

  • @brandoncfrey
    @brandoncfrey3 жыл бұрын

    Not a clue what most of what you said means on a technical level. But still couldn't stop watching. Great video. C++ gods help me 😭

  • @alexstone691
    @alexstone6913 жыл бұрын

    First time somebody explained this and i managed to understand it, thank you!

  • @davidm.johnston8994
    @davidm.johnston89943 жыл бұрын

    That's amazing, thank you so much Mr Cherno! By the way, a lot of what I know of C++ I've learned from you, so a huge thanks, this has been very helpful! (Shoutout to Corey Schafer too for teaching me Python!)

  • @learnprogrammingwithsean9010
    @learnprogrammingwithsean90103 жыл бұрын

    I've learned a lot from your vids, thanks for posting!

  • @fabricedelannay7288
    @fabricedelannay72882 жыл бұрын

    14:05 distinction between constructor and assignement operator, that's why I rather prefer to use {} brackets with new object creation and use the = only for assignement operator :) (For instance : String name{"Cherno"};) By the way, huge and awesome work, thanks !

  • @Drastonar
    @Drastonar3 жыл бұрын

    This is something I did so many times in C, but my teachers never told me it was a concept (move). It's much clearer when to do what now and the class abstraction we can do in C++ helps the code to be cleaner.

  • @maysanghai123

    @maysanghai123

    2 жыл бұрын

    I think what you must have been doing is a simple typecasting in school. This is just a specific version of typecasting which works specifically with rvalue references.

  • @paulchoudhury2573
    @paulchoudhury2573 Жыл бұрын

    Excellent presentation. Well organized and full of important details.

  • @thestarinthesky_
    @thestarinthesky_3 жыл бұрын

    Thank you Cherno. I enjoyed learning every second of this video and I realized that the pace you spoke today in this video was quite perfect! Not too fast! I appreciate that!🥰😍🥰😍🤗❤️

  • @joebosah2727
    @joebosah27273 жыл бұрын

    Shall get there and will overcome Thank you, Cherno

  • @z0lol
    @z0lol3 жыл бұрын

    great video! well explained. would be great having a video regarding Variadic functions :D

  • @JaiHall
    @JaiHall3 жыл бұрын

    Very insightful, thank you. 🙏🏿

  • @Dante3085
    @Dante30853 жыл бұрын

    i literally have been waiting and researching this the last few days. nice !

  • @spicy_wizard
    @spicy_wizard3 жыл бұрын

    perfect forwarding ~

  • @TheDada0106
    @TheDada0106 Жыл бұрын

    You're a legend, learning c++ with you is exciting !

  • @SwissPhil02
    @SwissPhil02 Жыл бұрын

    this is really well explained! Wish I would have watched your series before my interview...

  • @TheMR-777
    @TheMR-7773 жыл бұрын

    Wow! Really Nice! Many Thanks 4 this concept!

  • @user-vs6kl8ph5v
    @user-vs6kl8ph5v Жыл бұрын

    thanks for the difference between = and move constructor part, really never thought about it!

  • @deepikagoyal07
    @deepikagoyal07 Жыл бұрын

    Move semantics is a topic difficult to comprehend. Your in depth explanation made it very clear.

  • @niekbeijloos8355
    @niekbeijloos83552 жыл бұрын

    Great Videos you make! Keep it up!

  • @hakoo2700
    @hakoo27003 жыл бұрын

    thanks for this amazing series

  • @Rohith_E
    @Rohith_E3 жыл бұрын

    Your examples make move semantics really easy to understand. Please explain copy and swap idiom too.

  • @anonb1150
    @anonb11503 жыл бұрын

    Amazing video! You taught me how to create a move constructor and move assignment operator for my matrix class! Thank you!

  • @vaibhavlohiya8396

    @vaibhavlohiya8396

    Жыл бұрын

    If you still have that code can you send me the github repo coz I'm interested. Thank you

  • @aleksandarfranc1094
    @aleksandarfranc10942 жыл бұрын

    You are a legend man!!

  • @apelsin3001
    @apelsin3001 Жыл бұрын

    Thank you much! very understandable!

  • @coolbrotherf127
    @coolbrotherf127 Жыл бұрын

    The move command is extremely useful when using smart pointers. Moving vectors of unique pointers around without having to copy a bunch of stuff is extremely useful.

  • @sakithanavod9289
    @sakithanavod92893 жыл бұрын

    Cant wait till next video 😍

  • @ameynaik2743
    @ameynaik27432 жыл бұрын

    1. Instead of casting using (T &&)var you can use std::move(var) 2. A good ways to understand std::move() is that you are basically saying "I don't need the object that I am passing anymore. I am allowing someone to steal from it." 3. We need to always delete the current data before shallow copying the other data. Also we need to point other data to nullptr.

  • @malborboss

    @malborboss

    2 ай бұрын

    4. Before moving the other object into ours we need to ensure that we are not moving the same object to itself. Otherwise data may be lost.

  • @farhan787
    @farhan7873 жыл бұрын

    Cherno finally a new video for C++ series, thanks ♥️

  • @tiantianliu5958
    @tiantianliu59584 ай бұрын

    Great! keep going!

  • @adityanandagudi
    @adityanandagudi2 жыл бұрын

    awesome!! Thanks buddy

  • @marcofeder6768
    @marcofeder67683 жыл бұрын

    Nice video. I like to think to std::move as a way to have an rvalue reference. I remember that Stroustrup wrote in "The C++ Programming Language" that the best name for std::move() would have been rval()

  • @alecmather
    @alecmather Жыл бұрын

    Please never stop making videos.

  • @thestarinthesky_
    @thestarinthesky_3 жыл бұрын

    The more I learn about C++, the more I realize that I know NOTHING about this complicated but amazing language! I could learn C++ for the rest of my life and still there would be much to know! 🤩😍❤️🥳

  • @__jan

    @__jan

    3 жыл бұрын

    that's not a good thing

  • @thestarinthesky_

    @thestarinthesky_

    3 жыл бұрын

    @Prochy it is just the matter of preferences☺️ I would love to be challenged and C++ is a REAL programming language! C++ helps me to realize what is happening behind the hood, C++ gives insight into things I can’t see in other language! C++ is a miracle in the world of computer science! C++ being huge and big, makes you keep learning and learning! There will be always something new and challenging for you! That’s what I want! Never stop learning! 🙏❤️

  • @oracleoftroy

    @oracleoftroy

    3 жыл бұрын

    @@__jan Some people like dead languages that never change, others like new features that improve their code clarity and performance. I'm of the opinion that C++ being an actively improved language is a very good thing, and having to continually learn new things is part of the job description of any software developer.

  • @jonathanp5195
    @jonathanp51952 жыл бұрын

    Thank you for the video. I think what "clicked" hardest for me was when I learned that the assignment operator, by default, only moves the values inside an object to a different object, if the object that it is being moved into pre-existed - which is why we overload the assignment operator, so that we can do this, even when the object that the data is being moved into is being initialized. Or at least, this is how I think it works. If I'm misunderstanding, I would love some clarification.

  • @astronautid7948

    @astronautid7948

    2 жыл бұрын

    Good point! In a way the moving is the best thing that can happen to m_Data inside that operator. And we're talking the move assignment operator and the moving as opposed to copying which is what memcpy does in the copy constructor.This line says it all: m_Data = other.m_Data; Don't get disillusioned, however, thinking the data is being copied here. It's only a pointer being copied to another pointer. Imagine m_Data (in other) points to a char array the size of like billion. The array needn't to be copied, it stays where it was in memory. That's when we like assignment by move better than by copy. And we for this reason overload the move assignment operator. You got the gist.

  • @kelvinsmith4894
    @kelvinsmith48943 жыл бұрын

    Awesome work you’re doing here!!, do you plan on making videos on error handlings and file system read and write in C++?

  • @user-cy1rm5vb7i

    @user-cy1rm5vb7i

    3 жыл бұрын

    file read and write is rather simple, use , open a file and use it as cout or cin

  • @SirShrimpey
    @SirShrimpey3 жыл бұрын

    Dope, some time ago I was looking for a nice explanation of move assignment operator and exisiting stuff was quite confusing, I could have used a tutorial like this.

  • @VladTananaev

    @VladTananaev

    3 жыл бұрын

    I believe explanation in chapter 5 in "Effective Modern C++" by Scott Meyers is one of the best source I've read on this topic. I have a paper copy, but it seems you can find it for free on the internet.

  • @varuntaneja7073
    @varuntaneja70733 жыл бұрын

    I don't earn right now so can't really support you on patreon but the least I can do is watch all the ads on your videos :)

  • @kartikgarasia5685

    @kartikgarasia5685

    3 жыл бұрын

    Man now I just feel bad.. I also should pause adblocker on this channel

  • @RogerTannous
    @RogerTannous Жыл бұрын

    I like the sentence "steal everything from Apple" 🤣

  • @xi_the_pooh
    @xi_the_pooh2 жыл бұрын

    Maybe covering std::forward here as well would've been a good idea, since they're somewhat related.

  • @Evan490BC

    @Evan490BC

    2 жыл бұрын

    Yes, I was going to suggest the same.

  • @JorgeLuis-ts6qp

    @JorgeLuis-ts6qp

    2 жыл бұрын

    Would you recommend any particular resource to learn that?

  • @luyuanqi2960
    @luyuanqi29602 жыл бұрын

    Dude you are a live saviour

  • @s187v
    @s187v Жыл бұрын

    thank you

  • @Kaslor1000
    @Kaslor10003 жыл бұрын

    This is insane, back to my simple Java world

  • @ericjovenitti6747
    @ericjovenitti6747 Жыл бұрын

    So one you’re an absolute amazing teacher and thank you for doing us, too maybe give use case examples for this I still factor five years don’t completely understand the best used cases for this. Is this more conducive for embedded systems or extremely low latency code?

  • @VoidloniXaarii
    @VoidloniXaarii10 ай бұрын

    Thank you very much

  • @unmeinks5907
    @unmeinks5907 Жыл бұрын

    after he said "implicit conversion and call into this specific string constructor" my furnature started floating, help

  • @charlesmola
    @charlesmola3 жыл бұрын

    Maybe you can do a video about Hash Tables!! it would be very useful. Thanks for the videos btw, they help me a lot

  • @SergeySuper_Silver
    @SergeySuper_Silver10 ай бұрын

    I'd like to add one more thing. In order to create an empty object (in this case String 'dest'), we need a default constructor which sets m_Size = 0 and m_Data = nullptr.

  • @etinosaizekor6533
    @etinosaizekor65332 жыл бұрын

    Awesome!

  • @glennstormdesign
    @glennstormdesign3 жыл бұрын

    SuperawesomethankyouYan!

  • @igniculus_
    @igniculus_3 жыл бұрын

    I can't believe he is still making C++ videos. This just shows how much there is to know about C++ and how little I know ... :P

  • @lordmushroom723

    @lordmushroom723

    3 жыл бұрын

    it's because this language has become extremely bloated and a pain in the ass to work with

  • @alpyre

    @alpyre

    3 жыл бұрын

    @@lordmushroom723 Words of wisdom. C++ has too many features to solve the issues that are there because of C++ itself.

  • @greatbullet7372

    @greatbullet7372

    3 жыл бұрын

    @@alpyre not really, it has just its rules and every new standard makes it easyer to create decent code

  • @RWM_

    @RWM_

    3 жыл бұрын

    @@greatbullet7372 Yes, like here when you must create specific constructors and operators :)

  • @igorszemela1610
    @igorszemela16102 жыл бұрын

    great vid

  • @Mohammed24441
    @Mohammed244413 жыл бұрын

    A video on lifetime extension, return value optimization, forwarding...

  • @jackofnotrades007
    @jackofnotrades0073 жыл бұрын

    Could you do a video on streams? iostream and fstream class heirarchy and some stream handling functions?

  • @marioterresdiaz6001
    @marioterresdiaz60012 жыл бұрын

    Great video! Got a question though, is it possible/recommended avoiding copying when the object is returned?

  • @IllumTheMessage
    @IllumTheMessage3 жыл бұрын

    Muchos gracias

  • @LahiruGunathilake
    @LahiruGunathilake3 жыл бұрын

    Hi Cherno, can you please do a video about std::forward too ? Thanks for this.

  • @travel_is_fun3455
    @travel_is_fun34553 жыл бұрын

    you are great

  • @fuzzyrock3146
    @fuzzyrock31463 жыл бұрын

    You are the best teacher on CPP topics ;)

  • @user-vx6zk5jj4e
    @user-vx6zk5jj4e3 жыл бұрын

    nice video

  • @lukenukem8028
    @lukenukem80283 жыл бұрын

    Knowing what to type is the complicated part.

  • @nagarjunvinukonda162
    @nagarjunvinukonda1623 жыл бұрын

    Can you make a video on push_back vs emplace_back used in move semantics when calling constructors.

  • @JATmatic
    @JATmatic3 жыл бұрын

    Rule of five.. rule of five! Please, explain this and do the string class with copy-ctor && swap() idiom! :)

  • @xxdeadmonkxx
    @xxdeadmonkxx3 жыл бұрын

    No one: Voice recorder: blinking green as Cherno say something

  • @buzzdx
    @buzzdx3 жыл бұрын

    very good explanation, thanks. i used to struggle understanding move semantics. one question though: in the string destructor you used "delete m_data;" while in the move operator you used "delete[] m_data". since you allocated the memory using new[], i think the destructor should use delete [] too. however it's not crashing. does it make a difference really which one you use to delete?

  • @oracleoftroy

    @oracleoftroy

    3 жыл бұрын

    It is undefined behavior. As you point out, he should be using operator delete[].

  • @SerBallister

    @SerBallister

    3 жыл бұрын

    In the case of POD types there are no destructors so new and new[] behave the same in this case. For none POD classes/destructable classes the compiler/implementation does do some trickery like encode the number of objects belonging to the array so it can later call the destructors on them all. It is best practice to delete[] though as some compilers/implementations may not simplify array allocations (most compilers do, but that's not a guarantee!)

  • @oracleoftroy

    @oracleoftroy

    3 жыл бұрын

    @@SerBallister It is undefined behavior and thus is illegal C++ and shouldn't be relied on. It might work on your machine, but not other machines, and not your machine tomorrow as compiler and standard library implementer figure out better ways to optimize your program. A simple example, an allocator might pool all allocations for a single byte into a pool of memory such that a call to `delete charptr;` would be assumed to come from this pool whereas `delete [] charptr;` would expect the object to be allocated in a different pool (perhaps a large object pool). As the implementer knows it is illegal to use `operator delete` for memory allocated with `operator new[]` they can avoid storing extra information that isn't needed for a legal program and are under no obligation to properly cleanup programs relying on undefined behavior. Being a primitive type or not having a constructor isn't a reason to accept undefined behavior in a program.

  • @baldendoboriqua6391
    @baldendoboriqua63913 жыл бұрын

    Very humbling video.. I don’t know shit ... 😅 I have so much to learn

  • @ankithmanjunath3052
    @ankithmanjunath30523 жыл бұрын

    HI Thank you for the amazing video. At 10:17 , you mention that "If they are different objects, then nothing needs to be done !", this should be rather "If they are the same objects, then nothing needs to done", am i correct ?

  • @Didi-si5cs
    @Didi-si5cs3 жыл бұрын

    Hi Cherno, would you please make some videos about c++ windows form / desktop application.

  • @judickaelsam3103
    @judickaelsam3103Ай бұрын

    Thank you so much. Can you do it for std:: forward please?

  • @reza-nr2es
    @reza-nr2es3 жыл бұрын

    wow the way i never knew that std::move is just a small template function which just simply does the casting thing for us...and all that casting does is calls the move constructor...the lazy and dumb me never tried to look up the definition of that std::move function....now i know...thanks cherno!!! but actually have a question...in order to use the move constructor, do we always explicitly have to tell the compiler by casting the type into a rvalue ref??? or there are cases where the compiler does it implicitly??? thanks in advance!!!

  • @maniaharshil
    @maniaharshil5 ай бұрын

    Please cover the forwarding and arguments decay too Thanks

  • @coreyzamprogno5510
    @coreyzamprogno55103 жыл бұрын

    Digging the sweaters lately Yan

  • @nawnwa
    @nawnwa2 жыл бұрын

    Everything was clearly explained, only place I could fully accep the use of std::move was in the Entity constructor with "String&& name" parameter, isn't name already a r-value reference of the type "String&&" in the member initializer list, why do we again have to convert it to an r-value reference using std::move?

  • @saiprasadchindam5185
    @saiprasadchindam51853 жыл бұрын

    Please do a video on memory allocators

  • @whythosenames
    @whythosenames3 жыл бұрын

    1:48 the classes are bytesized... that was a little nerdy but yeah i liked it :)

  • @gaborsohar7281
    @gaborsohar72813 жыл бұрын

    @The Cherno, please explain me why don't you have a return statement after line 46 (in the final version). The move assignment operator is returning *this when it's the same object, but nothing otherwise.

  • @simonmaracine4721
    @simonmaracine47213 жыл бұрын

    Now I realize that Rust is basically doing move semantics by default. And to do a deep copy, you have to clone() the variable. let mut dest = String::new(); let simon = String::from("Simon"); let other = String::from("other"); dest = simon; // This is a move; simon is now gone println!("{}", simon); // Compilation error dest = other.clone(); // Both dest and other are available println!("{}", other); println!("{}", dest); // Works In C++ is the other way around. By default deep copying is used.

  • @_slier
    @_slier3 жыл бұрын

    Hi Yan..what your thought on Rust..alot of people love Rust but honestly, the only things i like about Rust is its package manager ( Cargo )..other than that, i dont see any benefit from using it...its syntax is maddness...

  • @scoutthespirit1133
    @scoutthespirit11333 жыл бұрын

    have you heard of Jai programming language,? id be willing to help out any way i could to see your professional thoughts on a new language like that, it cuts out alot of standard things from c++

  • @sumedhkulkarni5346
    @sumedhkulkarni53462 жыл бұрын

    Hi, Awesome explanation, 😀 one question, why not pass objects by reference if want to give the ownership of the object to the function? I know that move will invalidate the first object and literally move it in the function space. but in a case, where we just want to avoid copy while passing to the function and the application is single-threaded even the pass by reference should work right?

  • @vaibhavlohiya8396

    @vaibhavlohiya8396

    Жыл бұрын

    That 's what he is doing isn't it. He is using the rvalue reference i.e (String&&) to move the object, similar to what he did in the Entity class in the previous video. And instead explicitly casting to the object, he is using the std::move. Furthermore, just passing it by reference will only be valid for lvalues, now the work around for that is const reference but for that also you have to copy the data. I hope this helps.

  • @XQuanten
    @XQuanten3 жыл бұрын

    This is off topic but how do you use Italic font in VS?

  • @ILoveSoImAlive
    @ILoveSoImAlive2 ай бұрын

    perhaps would be correct to first say, that move is for performance optimization. there is nothing wrong with copying in most cases. performance optimizations must only be done based on performance requirements. there is no use of doing that where its not needed at all, because performance requirements are met anyways. it would make your product worse, because it would get more expensive, without getting perceivably and practically better. its like if I would put 30% more work into the car, by giving decorative painting to all the screws inside the car.

  • @Guru4hire
    @Guru4hire3 жыл бұрын

    Do you think a video on std::forward is possible?

  • @AbdullahJirjees
    @AbdullahJirjees3 жыл бұрын

    Hello and thank you for the video I am having this question assignment can anyone help me in this issue? interval_map is a data structure that efficiently associates intervals of keys of type K with values of type V. Your task is to implement the assign member function of this data structure, which is outlined below. interval_map is implemented on top of std::map. In case you are not entirely sure which functions std::map provides, what they do and which guarantees they provide, we provide an excerpt of the C++ standard here: Each key-value-pair (k,v) in the std::map means that the value v is associated with the interval from k (including) to the next key (excluding) in the std::map. Example: the std::map (0,'A'), (3,'B'), (5,'A') represents the mapping 0 -> 'A' 1 -> 'A' 2 -> 'A' 3 -> 'B' 4 -> 'B' 5 -> 'A' 6 -> 'A' 7 -> 'A' ... all the way to numeric_limits::max() The representation in the std::map must be canonical, that is, consecutive map entries must not have the same value: ..., (0,'A'), (3,'A'), ... is not allowed. Initially, the whole range of K is associated with a given initial value, passed to the constructor of the interval_map data structure. Key type K besides being copyable and assignable, is less-than comparable via operator is bounded below, with the lowest value being std::numeric_limits::lowest() does not implement any other operations, in particular, no equality comparison or arithmetic operators Value type V besides being copyable and assignable, is equality-comparable via operator== does not implement any other operations You are given the following source code: #include #include template class interval_map { std::map m_map; public: // constructor associates whole range of K with val by inserting (K_min, val) // into the map interval_map( V const& val) { m_map.insert(m_map.end(),std::make_pair(std::numeric_limits::lowest(),val)); } // Assign value val to interval [keyBegin, keyEnd). // Overwrite previous values in this interval. // Conforming to the C++ Standard Library conventions, the interval // includes keyBegin, but excludes keyEnd. // If !( keyBegin // and assign must do nothing. void assign( K const& keyBegin, K const& keyEnd, V const& val ) {

  • @heisenburger311
    @heisenburger3113 жыл бұрын

    Hi, Cherno. Thank you very much for your course. I have a question. Both `delete[] m_Data;` and `delete mData;` exist in your code, which I don't know the difference. Can you kindly tell me when to use which one.

  • @inx1819

    @inx1819

    3 жыл бұрын

    Inside the ~String() method he should've written delete[] m_Data instead. The delete keyword alone deallocates a single object from memory, but the delete with [] deletes a whole heap allocated array.

  • @carnaqe1154
    @carnaqe11544 ай бұрын

    how can you Print the uninitialized m_data member without an error?

  • @sergei8337
    @sergei83373 жыл бұрын

    what's the benefit over usage of pointer/refs here? In other words why move if we can just refer to the data?

  • @johnc4624
    @johnc462411 ай бұрын

    Your string(string&&) constructor assigns to members instead of initialising. Was that on purpose? (4:20) Haven't heard the rest - thanks for making the video :-)

  • @AbhishekKumar-el3ls
    @AbhishekKumar-el3ls5 ай бұрын

    While overloading the move assignment operator, should we not allocate memory for m_Data? If the destination string length is greater than the original string we are moving into, there would be a memory access violation. String orig = “Hello”; String dest = “LongerString”; orig = std::move(dest); can somebody explain?

  • @myronkipa2530
    @myronkipa2530 Жыл бұрын

    Cpp devs learning basic rust concepts

  • @belovedlamp8998
    @belovedlamp89983 жыл бұрын

    My fucking hero

  • @lucagagliano5118
    @lucagagliano51183 жыл бұрын

    I understand the basic idea. But I can't really figure a realistic case where I need to implement these explicitly. Usually you want to avoid heap allocation and people use a lot the stl library. Unless you're writing your own library I can't really picture a situation where you need to implement move constructors and move assignment explicitly. You also have default move constructors, but I don't know how they works.

  • @UsernameUsername0000

    @UsernameUsername0000

    9 ай бұрын

    2 years late. You’re right that the STL encapsulates functionality. In cases where your class is composed of STL containers and/or smart pointers, then the default move constructor generated by the compiler would suffice. It does a member-wise move of all the members. If you use modern C++ and are more of a consumer than a creator, then you would likely never have to explicitly define those functions. Still, it’s good to be aware of the specifics of move semantics to allow you to leverage them well.