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

The Cherno
19 Jun 202016:06

Summary

TLDRIn this C++ tutorial video, Deshawn continues the series on move semantics, focusing on two crucial aspects: `std::move` and the move assignment operator. He explains `std::move` as a function that converts an object into an r-value, enabling move operations. Deshawn then demonstrates how to implement the move assignment operator for efficient resource transfer between objects. The video also highlights the difference between move constructors and assignment operators, emphasizing the importance of these concepts in modern C++ programming. Viewers are encouraged to explore the topic further through the series and take advantage of a special Skillshare offer for learning new skills.

Takeaways

  • πŸ“š The video is part of a C++ series focusing on move semantics, continuing the journey from previous episodes.
  • πŸ”— The host encourages viewers to watch prior videos on move semantics and L values for better understanding.
  • 🎯 The main topics for this video are `std::move`, the move assignment operator, and their applications in C++ programming.
  • πŸ› οΈ The `std::move` function is introduced as a utility to efficiently move resources from one object to another without copying.
  • πŸ”„ `std::move` is used to cast an object to an r-value reference, enabling the use of move constructors or move assignment operators.
  • πŸ“ The move assignment operator is explained as a special assignment operator used to move resources into an existing object.
  • πŸ’‘ The video demonstrates how to implement the move assignment operator in a custom class, emphasizing the importance of handling self-assignment.
  • ⚠️ A key point is made about the difference between using `std::move` for an existing variable versus a temporary, and its implications on resource management.
  • πŸ”‘ The concept of the 'rule of fifths' or 'rule of thirds' is mentioned, indicating a set of best practices including move constructors and move assignment operators.
  • πŸ“ˆ The video includes a practical example of implementing and testing a move assignment operator in a custom string class.
  • 🎁 The host promotes Skillshare as an online learning platform offering a wide range of creative classes, with a special offer for the viewers.
  • πŸ‘‹ The video concludes with a summary of the move assignment operator and `std::move`, and an invitation for viewers to engage with the content and ask questions.

Q & A

  • What is the main topic of Deshawn's C++ series video?

    -The main topic of the video is move semantics in C++, specifically focusing on std::move, the move assignment operator, and how to implement them in custom classes.

  • Why is understanding move semantics important for C++ programmers?

    -Understanding move semantics is important because it allows for efficient transfer of resources between objects without unnecessary copying, which can improve performance and resource management in C++ programs.

  • What is the purpose of std::move in C++?

    -The purpose of std::move is to efficiently transfer the contents of an object to another, effectively 'moving' the resources and leaving the original object in an unspecified but valid state.

  • What is the difference between a move constructor and a move assignment operator?

    -A move constructor is used to initialize a new object with the resources of a temporary object, while a move assignment operator is used to transfer the resources from one existing object to another, modifying the existing object in the process.

  • Why is it necessary to check if the move assignment is happening to the same object in the move assignment operator?

    -It is necessary to check because moving an object into itself would lead to self-assignment, which can cause issues like memory leaks if not handled properly by first nullifying the current object's resources.

  • What does the rule of five in C++ include, and why is it important for classes that manage resources?

    -The rule of five includes the destructor, copy constructor, copy assignment operator, move constructor, and move assignment operator. It is important for classes that manage resources to ensure proper handling of object lifetimes and resource management.

  • How does the move assignment operator differ from the copy assignment operator in C++?

    -The move assignment operator transfers ownership of resources from one object to another, potentially leaving the source object in a valid but unspecified state, while the copy assignment operator creates a copy of the resource, which is generally more expensive in terms of performance.

  • What is the role of 'noexcept' in the move assignment operator?

    -The 'noexcept' specifier in the move assignment operator indicates that the function is not expected to throw exceptions. This can help with optimization and exception safety in C++ programs.

  • Why is Skillshare mentioned in the video, and what does it offer to its users?

    -Skillshare is mentioned as a sponsor of the video. It offers an online learning community with thousands of classes on various creative topics, allowing users to learn new skills at their own pace with bite-sized video lessons.

  • What is the significance of the 'rule of thirds' and 'rule of fifths' in C++ programming?

    -The 'rule of thirds' refers to the three special member functions (constructor, copy assignment operator, and destructor) that need to be properly defined for classes managing resources. The 'rule of fifths' extends this to include move constructors and move assignment operators, which are important for efficient resource management in modern C++.

Outlines

00:00

πŸ”§ Introduction to Move Semantics

Deshawn welcomes viewers back to his C++ series and emphasizes the importance of understanding move semantics. He references previous videos on the topic and introduces the concepts of the STD move function and the move assignment operator, promising to cover both in this video.

05:01

πŸ› οΈ Understanding STD Move

Deshawn explains the STD move function, demonstrating its purpose in converting objects to temporary r-value references. He contrasts manual casting with the more elegant and flexible STD move function, highlighting its role in move semantics.

10:02

πŸ“¦ Implementing Move Assignment Operator

The move assignment operator is introduced as a crucial component of move semantics, particularly for assigning existing objects. Deshawn shows how to define this operator, ensuring proper memory management and avoiding self-assignment issues.

15:03

πŸ’‘ Practical Example and Conclusion

A practical example using the move assignment operator is presented, demonstrating its effectiveness in transferring resources without copying. Deshawn emphasizes the importance of including both move constructors and move assignment operators in classes. He concludes by encouraging viewers to explore more on move semantics and inviting them to check out Skillshare for learning new skills.

Mindmap

Keywords

πŸ’‘Move Semantics

Move semantics is a feature in C++ that allows for efficient transfer of resources from one object to another by 'moving' them, rather than copying. In the video, the concept is central to understanding how to efficiently manage resources, especially when dealing with temporary objects or when reassigning resources to existing objects. The script discusses how to implement move semantics in custom classes to prevent unnecessary copying and to improve performance.

πŸ’‘R-value references

R-value references in C++ are a type of reference that can bind to temporary objects. They are denoted by an ampersand prefix followed by two ampersands (&&). The script uses r-value references in the context of move constructors and move assignment operators, which are designed to take advantage of r-values to perform moves instead of copies.

πŸ’‘L-values and R-values

L-values and R-values are classifications of expressions in C++. An L-value refers to an object that persists beyond a single expression, allowing its address to be taken, while an R-value represents temporary objects that do not have a persisting address. The video script refers to these concepts to explain the conditions under which move semantics can be applied.

πŸ’‘std::move

std::move is a function in C++ that is used to cast an object to an r-value reference, indicating that the object can be safely moved from. In the script, std::move is discussed as a utility function that enables the move constructor or move assignment operator to be invoked, facilitating the transfer of resources from a temporary or existing object.

πŸ’‘Move constructor

A move constructor is a special constructor in C++ that takes an r-value reference as its parameter, allowing the transfer of resources from a temporary object to a newly constructed object. The script explains how to implement a move constructor in a custom string class to optimize the transfer of data without unnecessary copying.

πŸ’‘Move assignment operator

The move assignment operator is a member function of a class that overloads the '=' operator to handle the transfer of resources from one object to another when the right-hand side is an r-value. The script demonstrates how to implement this operator to move resources from one string object to another, leaving the source object in a valid but unspecified state.

πŸ’‘Skillshare

Skillshare is an online learning community mentioned in the script as a sponsor of the video. It offers a wide range of classes on creative topics, and the script highlights its value for learning new skills. The mention of Skillshare serves as an example of how educational platforms can complement technical learning, such as C++ programming.

πŸ’‘Rule of Three

The Rule of Three in C++ is a guideline that suggests that if a class requires a custom copy constructor, copy assignment operator, or destructor, it should also provide the other two. The script refers to an extension of this rule, the Rule of Five, which includes move constructors and move assignment operators, emphasizing the importance of considering all special member functions for resource management.

πŸ’‘Destructor

A destructor in C++ is a special member function of a class used to free resources and perform cleanup when an object's lifetime ends. The script discusses the role of the destructor in the context of move semantics, ensuring that no resources are leaked after a move operation by properly deallocating memory.

πŸ’‘Self-assignment

Self-assignment occurs when an object is assigned to itself. The script mentions the importance of handling self-assignment in the move assignment operator to avoid unnecessary operations that could lead to undefined behavior or resource leaks. It suggests checking if the source and destination objects are the same before performing the move.

Highlights

Introduction to move semantics and the importance of previous videos in the series.

Explanation of STD move and its usage.

Introduction to the Move assignment operator.

Difference between move constructors and move assignment operators.

Example of using STD move with a string object.

Importance of making an object temporary for move semantics.

Code example showing move assignment operator implementation.

Explanation of deleting old data to avoid memory leaks.

Importance of checking for self-assignment in move assignment operators.

Testing move assignment with a string example.

Difference between assignment operators and constructors.

Use of STD move to convert an existing variable into a temporary.

Rule of fifths and its relevance to move semantics.

Distinction between move constructors and move assignment operators.

Encouragement to use STD move for converting objects to temporaries.

Transcripts

play00:00

as of guys my name is Deshawn and

play00:01

welcome back to my C++ series so today

play00:04

we're gonna be continuing on with our

play00:06

whole move semantics journey if you guys

play00:08

have not seen the other videos in this

play00:10

kind of a miniseries and definitely

play00:12

check them out I'll have one linked up

play00:13

there that's going to be about move

play00:15

semantics in general but there's also

play00:17

another video about L values and our

play00:18

values for sure you know this whole

play00:20

moving journey make sure that you check

play00:22

those videos out because they're going

play00:24

to be important for what we'll be

play00:25

discussing today so so far we've we've

play00:28

basically learned the the gist and all

play00:30

of the basics of move semantics what it

play00:33

means to actually be able to move one

play00:35

object into another however we haven't

play00:38

covered a crucial part of that or should

play00:41

I say two crucial parts and instead of

play00:43

making two videos one on each topic I

play00:45

figured I'd combine them into this video

play00:47

because they're not particularly

play00:49

complicated topics and I think we can

play00:50

just get them done in one video and that

play00:52

is STD move so what is that function

play00:54

what does it do when do we use it when

play00:57

do we need to use it and then also

play00:59

something called the Move assignment

play01:01

operator and this is specifically an

play01:03

assignment operator that we use when we

play01:05

want to remove an object into an

play01:07

existing object so we've covered move

play01:09

constructors or a move constructor

play01:12

that's what we did in the last episode

play01:13

however what we haven't covered is what

play01:16

happens if we instead of constructing a

play01:18

new object we want to move an existing

play01:20

object into another existing object and

play01:22

that's what we're going to be talking

play01:24

about today but first this video is

play01:26

sponsored by skill share if you guys

play01:28

have not heard of skill share Skillshare

play01:30

is an amazing online learning community

play01:32

where millions of creative and curious

play01:34

people come together into a brilliant

play01:36

community to learn something new they've

play01:39

got thousands of amazing classes on

play01:41

pretty much any creative topic you can

play01:43

think of and one of the best parts well

play01:46

at least in my opinion is that the

play01:47

classes are also short all the videos

play01:50

are so bite-sized that you can actually

play01:52

watch them in the spare five minutes you

play01:55

might have throughout your day which i

play01:57

think is amazing lately I've been

play01:59

wanting to maximize my productivity and

play02:01

motivation and they've got a lot of

play02:02

excellent classes on that topic as well

play02:04

as general business classes which is

play02:06

great and for just under ten dollars a

play02:08

month for an annual subscription I think

play02:09

it's great value but Skillshare actually

play02:12

during the first 1,000 people who click

play02:14

the link in the description below 2

play02:16

months of free Skillshare premium so

play02:18

definitely check that out for free and

play02:19

get started right now because a great

play02:21

platform and is a really good way to

play02:23

learn a new skill speaking of learning

play02:25

new skills let's get back to the move

play02:27

assignment operator and as city you move

play02:29

as always the best way to show this is

play02:31

gonna be with an example so let's dive

play02:33

into the code from last from the last

play02:35

episode where we looked at move

play02:36

semantics in general and let's upgrade

play02:38

our string and NC class to be able to

play02:40

handle move assignments and we'll also

play02:43

be talking about SUV move while doing

play02:45

that since that's an important part so

play02:46

this is the string class from the last

play02:48

video and here's our NC class as well

play02:50

let's first talk about STD move and what

play02:53

it does I'll actually use string as an

play02:55

example here just because we seem to

play02:57

have this kind of we where we have two

play02:58

classes we have string we have empty and

play03:00

entities like an extra level of

play03:02

indirection entity itself in fact is an

play03:04

example showing how to use string which

play03:06

is the real thing we're interested in

play03:08

moving so what happens here when we

play03:11

decide to actually take in a string as a

play03:13

temporary here is we're using STD move

play03:16

and this is from the last video we had

play03:18

to use it because this was a way for us

play03:20

to move this name temporary which were

play03:23

providing over here into this kind of

play03:26

more or less permanent spot for it to

play03:28

live in if I simplify this by writing it

play03:30

over here I'll create a string and I'll

play03:33

just set it to hello over here so now I

play03:35

have a brand new string what happens if

play03:38

I want to remove it into here maybe this

play03:40

is the destination for my move well I

play03:42

could assign it like this however what

play03:44

that's doing is copying the string into

play03:47

this new destination variable of course

play03:49

so what do I do if I want to

play03:51

specifically move it into this new

play03:54

variable well I have to make it into a

play03:57

temporary because that's what the move

play04:00

constructor takes right if we look at

play04:02

the string constructor here we have this

play04:04

one which is our move constructor this

play04:06

takes in and our value reference which

play04:08

is a temporary object so in other words

play04:11

it's really simple I have to make it use

play04:14

this constructor because this little

play04:16

blob of code here is what actually

play04:18

steals the resources from other so in

play04:21

other words that's the thing that

play04:22

actually does the move so in order for

play04:24

it to use that can

play04:26

tractor I need to actually make sure

play04:28

that this string this hollow string

play04:30

becomes a temporary now and how do we

play04:33

convert from one type into another

play04:35

generally as he was bus we cast so what

play04:37

I could do is I could cast this to be a

play04:40

string temporary like this and what's

play04:42

happening here is it's now constructing

play04:44

a new string with a constructor that

play04:46

takes in a string r-value reference here

play04:49

so in other words this is the same as if

play04:51

I had just written code like this

play04:52

because of course that assignment

play04:54

operator is just doing an implicit

play04:55

conversion and call into this specific

play04:58

string constructor so this is the code

play05:00

that I end up with I'm now moving this

play05:02

string into this new destination however

play05:05

this is a little bit well it's not the

play05:09

most elegant way of doing this and it

play05:11

also doesn't work with every single type

play05:13

for example if we just had something

play05:15

that was order and the type couldn't be

play05:16

deduced by ourselves writing the code

play05:18

static like this it's just not not not a

play05:20

great way to actually write code like

play05:21

this so what we do instead is we use a

play05:23

slightly more flexible function that

play05:25

basically at compile time figures out

play05:27

what type is coming in and that function

play05:30

is called sv move so what this is is

play05:32

just a utility function provided to us

play05:34

by the standard C++ library that will

play05:36

basically do what I just did but in a

play05:39

slightly more elegant and flexible way

play05:41

if we hit f12 over here individual

play05:43

studio to get to it you can see what's

play05:45

actually happening here it's a

play05:46

consecration it returns a remove

play05:49

reference T type of T Y and you can see

play05:51

there's that our value reference here so

play05:53

it takes in an argument here and it will

play05:55

actually return that double reference

play05:57

for us but it does so in a nicely

play05:59

templated way and it will work properly

play06:01

with all types including Const and all

play06:03

of that stuff so instead of doing what I

play06:05

did you should use STD move and it also

play06:08

looks a little bit more elegant in my

play06:09

opinion because it also gives a little

play06:11

bit of context as to what's going on in

play06:13

this case we're moving this string into

play06:15

this destination now I want to emphasize

play06:18

here that it doesn't really matter how

play06:19

you write this code I could just have an

play06:21

assignment operation going on over here

play06:23

the thing is we're creating a new object

play06:25

here this is not a move assignment that

play06:27

is taking place here because as you can

play06:29

see we've got the variable type out

play06:31

front here and then the variable name

play06:33

this is a brand new object that is being

play06:35

constructed and as such it will actually

play06:37

use the move

play06:39

octa now this leads into the movie Simon

play06:42

operator which is what I talked about an

play06:43

assignment operator which I don't think

play06:46

we've covered in general in this A+ for

play06:47

Syria so that's definitely something

play06:48

that will do soon but the assignment

play06:50

operator only gets called when we

play06:53

actually assign a variable into an

play06:57

existing variable so in other words if

play07:00

we now wrote something like desc equals

play07:02

string or something like that or more

play07:05

specifically STD move string to actually

play07:07

move it in this case what would actually

play07:09

happen is the assignment operator would

play07:11

get called because this is already an

play07:12

existing variable don't forget that an

play07:15

operator is really just like a function

play07:17

and so when you actually call the equals

play07:21

kind of operator here which is the

play07:23

assignment operator on an existing

play07:24

variable it's kind of the same as if you

play07:26

just had like a little assigned function

play07:29

and did something like that clearly this

play07:30

is happening on an existing variable

play07:32

whereas over here like it wouldn't

play07:35

really work because we're creating a new

play07:37

variable so let's go up and define this

play07:39

assignment operator so a regular

play07:40

assignment operator is something that

play07:41

we'll save for a different video of

play07:43

course because is about the move

play07:44

assignment operator but basically it's

play07:46

going to be very very very similar to

play07:48

the move constructor so this is going to

play07:50

be a good starting point what I'll do is

play07:52

I'll just copy all of this stuff here

play07:54

and then I'll go down here and write

play07:56

string reference so this is going to

play07:58

return a string reference will write

play08:00

operator equals which is our assignment

play08:02

operator and then it's going to take in

play08:04

that temporary object here we'll also

play08:07

mark it as no except I'm gonna paste in

play08:09

all of our code here and for the most

play08:11

part that is basically it however there

play08:14

is one very important thing missing here

play08:16

what we're doing is we're not actually

play08:19

constructing a new object like we were

play08:21

in the move constructor we're actually

play08:23

moving another object into ourselves

play08:27

into this current object so clearly we

play08:30

need to actually overwrite the current

play08:32

object because the current object might

play08:34

have some memory allocated and obviously

play08:37

if we just do this if we just set m data

play08:39

equal to another pointer we've created a

play08:41

memory leak because there's no way for

play08:42

us to actually delete that old data so

play08:46

what we need to do is first of all

play08:48

delete our current data because we're

play08:51

about to move another object into our

play08:52

cell

play08:53

and then we can reassign all of our

play08:56

variables and also make sure that the

play08:58

other object is left in a valid state so

play09:00

this is basically the most important

play09:02

thing that we can add now typically in

play09:05

assignment operators you also want to

play09:06

make sure that this object that this

play09:09

current object is not equal to another

play09:11

object so in other words if we try to do

play09:14

something somewhat silly such as

play09:16

assigned destination to itself using

play09:19

something like this right or I guess

play09:21

more specifically we'd have to put it

play09:22

into here if you try and do something

play09:24

like this then this will not really work

play09:28

because obviously what we're doing here

play09:30

is we're deleting data and then we're

play09:32

just reassigning everything I mean

play09:34

what's gonna what what's gonna end up

play09:35

happening is we just delete the data and

play09:37

we've lost the data and that's it sighs

play09:40

we'll still be kind of correct though so

play09:42

it's in in general it's just gonna mess

play09:44

everything up what we could do and

play09:46

specifically the right answer here is to

play09:48

just not do anything in this case

play09:50

because if these objects are equal then

play09:52

there's no removing necessary I mean

play09:55

you're trying to move this object into

play09:56

itself it's already there

play09:58

there's no removing necessary and the

play10:00

way we would typically implement this is

play10:01

just by saying if this equals other just

play10:05

like that and if you do something like

play10:07

that and specifically does not equal

play10:08

what we want if you if this is not equal

play10:12

to other so in other words these are in

play10:13

fact different objects then nothing

play10:15

needs to be done and we can just return

play10:17

this which is a reference to the current

play10:20

object so in other words we only do the

play10:22

move if they're not equal if they are

play10:25

this same object literally and we don't

play10:27

need to do anything if they're different

play10:29

objects but the data is different you

play10:31

still need to do the move because

play10:32

they're still moving some other object

play10:34

into yourself so this isn't really like

play10:36

the assignment operator where you doing

play10:37

a copy and if the data happens to match

play10:39

you don't need to do anything so you

play10:41

still need to do it and it's very easy

play10:42

to just implement a little a little god

play10:44

that does something like this so there

play10:47

we go that is a complete move assignment

play10:49

operator let's test it out so what I'll

play10:52

do is I'll make and to actually make

play10:54

this work we do have a default

play10:55

constructor for string which is good

play10:56

we're gonna kind of ignore the entity

play10:58

class for this example I think and what

play11:00

I'm going to do is basically come over

play11:02

here and I'll make a string here called

play11:05

well this will just be like our first

play11:07

I'll just call it Apple and I'll set it

play11:09

equal to Apple then I'll make another

play11:11

string called our kind of destination

play11:13

which is just going to be an empty

play11:15

string here and then what I'll do is

play11:17

I'll try and actually assign destination

play11:19

to the Apple now obviously this is going

play11:22

to cause an error here because we don't

play11:23

have an actual copy assignment operator

play11:26

so what we need to do is make sure this

play11:28

is wrapped in a move otherwise it's not

play11:30

even going to compile and what should

play11:31

happen here is we should move the

play11:33

contents of Apple in to destination and

play11:35

also leave Apple in a good State in a

play11:38

valid State so let's print the value of

play11:40

both of these that I'll print Apple

play11:42

child call Apple dot print and then

play11:44

destination dot print here and I'll also

play11:47

do so after we do the move and we'll see

play11:49

if the values have basically switched so

play11:52

in other words since we're stealing

play11:53

everything from Apple now it should be

play11:55

left with nothing and we shouldn't get

play11:57

any crashes or anything obviously upon

play11:59

destruction okay so and there's some

play12:01

other stuff going on here so I'm gonna

play12:02

quickly get rid of all this other noise

play12:06

okay so we have created them we we have

play12:09

Apple and this is I just realized that

play12:11

what I should also do is make sure that

play12:13

I'm actually printing the value of these

play12:16

very or rather the name of these

play12:17

variables because the moment we're only

play12:19

seeing the actual values okay so now if

play12:22

we run this we create the object Apple

play12:24

is set to Apple destination is set to

play12:27

nothing at all then we move it and now

play12:29

Apple is set to nothing at all and

play12:31

destination is set to Apple so we've

play12:33

basically transferred ownership of that

play12:35

entire array of characters without doing

play12:38

any copying or any allocating or

play12:40

deallocating or anything like that we've

play12:42

switched these two variables which is

play12:44

really good and amazingly powerful and

play12:46

that's it you can see both of the

play12:48

objects gets to get destroy successfully

play12:49

we don't get any crashes because in this

play12:51

case the Apple destructor is doing

play12:54

absolutely nothing since it's trying to

play12:56

delete an array over here that is set to

play12:59

null pointer and then the the

play13:02

destination destructor is is destroying

play13:05

our character successfully so that is

play13:07

the end of the move assignment operator

play13:10

and SCD move so in summary the move

play13:12

assignment operator is something that

play13:14

you want to include in your class when

play13:16

you include a move constructor because

play13:18

of course it might be desirable to

play13:20

an object into an existing variable it's

play13:23

basically part of something called the

play13:24

rule of fifths or something called the

play13:26

rule of thirds and then there's the rule

play13:28

of fifths which basically just includes

play13:29

all the new move semantics things we'll

play13:31

talk about that more in depth in later

play13:33

episodes of this equals law series as

play13:35

well one more thing I quickly want to

play13:37

show is that I did mention the

play13:38

difference between destination and the

play13:41

assignment operator versus if we had

play13:43

just used the constructor like this keep

play13:45

in mind that by using this operator it

play13:47

is literally the same as if we had

play13:49

written dot operator equals and called

play13:51

it like a function which is why clearly

play13:53

that would not work here because we're

play13:55

not actually calling the assignment

play13:57

operator we're just we're just

play13:58

constructing a new string that happens

play14:00

to use a temporary value which is why

play14:02

the move constructors being called

play14:04

whereas here we're actually calling a

play14:05

function so that's that that's an

play14:07

important distinction and one of the

play14:09

things that used to confuse me in the

play14:10

past because as you can see they both

play14:12

look like assignment operators but

play14:13

they're actually different this is the

play14:15

move constructor this is the assignment

play14:17

operator and STD move is what you want

play14:20

to do when you want to convert an object

play14:22

to be a temporary so in other words if

play14:25

you need a existing variable to become

play14:28

temporary in the sense that you're

play14:30

basically marking it as hey you can

play14:33

still resources from this particular

play14:34

variable this is what enables us to

play14:37

actually do the moves on an existing

play14:39

variable if you're creating a new

play14:41

variable and it's like in a function

play14:43

parameter or in the move constructor or

play14:46

something like that then it's already a

play14:47

temporary it's fine but if you've got an

play14:49

existing variable like with this Apple

play14:51

example that was an existing variable

play14:53

you need to make sure that you use sv

play14:55

move to basically convert it into a

play14:57

temporary and then that way you can

play15:00

actually use the move constructor or the

play15:02

move assignment operator to take the

play15:04

resources from that variable and

play15:06

actually do the move I hope you guys

play15:09

enjoyed this video please don't forget

play15:10

to hit the like button if you did if you

play15:13

have any more questions about move

play15:14

semantics in general hopefully these

play15:16

kind of past videos have cleared up the

play15:18

general complexity and the I mean I

play15:21

guess the the fact that it seems

play15:22

possibly more complicated and actually

play15:25

is hopefully all that has been cleared

play15:26

up for you but if there are still things

play15:27

that you want to know leave a comment

play15:29

below and I will try and help you out as

play15:31

well as the community but that's pretty

play15:32

much all there is to it

play15:34

smooth semantics I hope you guys enjoy

play15:36

don't forget to check out the free two

play15:37

months of Skillshare by clicking the

play15:39

link in the description below and I will

play15:40

see you guys next time goodbye

play15:44

[Music]

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
C++Move SemanticsProgrammingConstructorsAssignment OperatorsPerformance OptimizationSkillshareOnline LearningCode TutorialResource Management