welcome back to module 14 of uh programming in C++ we have been discussing about copy we have discussed about copy construction at depth and we have introduced what is copy assignment operator to quickly recap we do a copy construction when we want to make a clone of an object which does not exist and we do copy assignment when we have an existing object and want to copy another object of the same type into this existing object we have seen that a copy assignment can be defined as a function operator function and and it takes the parameter as a constant reference of the class and it returns a non-constant reference of the same class it may return a constant reference of the class as well now we will look into some of the some of the more uh tricky areas of copy assignment so you recall that uh in terms of copy construction we have already explained the notion of uh shallow copy and deep copy and now we'll have we will see that uh the consequences of shallow copy and deep copy also parets into copy assignment so particularly Focus this is a string example and focus on the copy assignment operator now what you are trying to do you are trying to so this is uh uh your let me just draw the two objects this is S1 this is S2 so this is uh football so whatever it will have it will have a length 8 this is cricet so this will have length 7 now I'm trying to copy so I'm trying to do S1 assigned S2 assigned S1 so if I copy this so naturally I will need to while I do copying I know that this string will have to be copied here and we know there are two options to that one is to copy the pointer other is to actually copy the object so we would like to do deep copy in this case we are doing a deep copy so we are making a s Str dope of the parameter s which is basically s Str dope of S1 now when we assign that to St Str then what will happen simply so another another football has got created s Str dup we have done duplicate so another football has got created now if I put this pointer into Str Str naturally I will lose this pointer and there will be no way to retrieve this string any further so before I can do this I have to free up otherwise the resource will leak memory will leak so this is a critical point so I'll first have to free this up and then I'm doing a string copy so as I free this up this is gone I do an Str Str dope so now I have a new football pointed to here this will get copied so this will become eight as in here and the object is returned as it was done in the last case just note that this basically Returns the start this is a current object so it returns that because it has to return the object to which the assignment is happening so this object can be now be used for chain assignment as I have explained the this can be used in the chain assignment as I have explained already so this is how a copy can be done for a string with deep copy that is similar strategy can be used whenever we have poter members in the object now let us look into a very small but dangerous issue with the code that we have already this is a code exactly the code that you have seen the only difference being the only difference being earlier we were copying S2 to S1 now I have copied S1 to S1 now you you can very legitimately ask me as to why should somebody write this kind of a code there are two answers to that one one is what if somebody writes we have to know what is going to happen the other issue is that not always the code will look like this for example it could be that I have a string I have a reference to S1 that is done somewhere I do not know where this is done this may have been done in some other function in some other class whatever it is come and now I'm doing is1 assigned R syntactically looking at the code it does not look like a self- copy but it actually is a self- copy right so self- copy is a is a is a potential situation that we must look into now certainly there are issues that the reason we are trying to look into this so look into this is a self- copy so this is what I have this is my S1 this is my string my S1 is football so I have football and uh I have eight here so now I'm doing S1 assigned S1 so what will happen this will execute first this is my S1 so this will free this up now this is this will try to do this that is it will try to take this object s. s make a copy make a copy into something and then assign it here now this object is already gone this has been freed up so what you make copy of here is not known it's not question mark it is just not known it's something invalid and then so on so quite expectedly what you get after the copy when you print it after the copy you get a garbage I got a garbage while I I was running it but it is quite possible that uh instead of a garbage it could be a crash because uh it just depends on what memory is getting validated so self- copy with pointer type of data is something which is quite uh which could prove to be quite uh difficult to deal with so we'll have to do something about that so the way we handle this and that's very typical is all that you want to say that if I'm doing a self- copy if I'm doing this then all that I need to tell my copy assignment operator is that do not copy if you're doing a self- copy then all that I would like to tell is do not copy because bypass I mean it's the same object so the rest of the code remains same but all that I add is check if it is the same object how do I check if it is the same object just understand S1 is being assigned S1 so it is S1 dot operator assignment S1 this is become s and this is the object on which the invocation has happened so this is start this so we want to see whether the start this and S are same we cannot compare objects like that because this could be any object I do not have a comparison operator for that these are not like integer that I can write equal to equal to but what I all that I know is if it the same object then it resides in the same memory so if these two have to be same then this has to be same as ERS and S their addresses have to be same if the addresses are same they're the same object if the addresses are different they're not the same object so all that you simply do is check if the addresses are different if the addresses are different you go through the copy if it is not then you simply bypass so this is a small point about self- copy in copy assignment operator that you should always keep in mind and this is a typical way to write a copy assignment operator particularly in the cases where you have poined type of data members the signature of the copy assignment operator we have already seen this is a these are typical signature and this is a basic structure that we have shown you first uh check for self copy then you release any resource that is currently held by the object being assigned to and then copy the rest of the members to the current object uh the it can be one of these it that is it is also possible that you do not use const you just do a copy without a constant on the parameter so which means that during copy actually the object you are copying from from can get changed and we'll see that this has a very serious uh use in terms of the design particularly in some uh Smart Designs known as smart pointers where this particular feature will be used extensively but we'll talk about that when the time comes and there are several other signatures which I've just listed them do not uh uh do not spend a lot of effort to understand or to memorize what these are these are allowed uh and these are used occasionally but they are very very occasional in situation so it's just that such copy assignment operators are possible but you will primarily use this and in some cases you will use this so to sum up here we have looked into copy conr structors where new object is created and this new object is initialized with the value of the data members of another object and the major requirement of copy construction happens for call by value and for initializing user defined types data members copy Constructors are to be provided by the user but if the user does not provide copy Constructor then the compiler will provide a free copy Constructor which just does a bit copy we have discussed about copy assignment operator which is simil which is doing a copy when the object is already existing so it can be it is already existing initialized then it has to be replaced by the members of the object being copied from and in copy assign M operator self- copy could be a significant issue and needs to be taken care of and again please keep in mind that uh this is not explicitly written in the slide but please keep in mind that if the user does not provide a copy assignment operator but uses it in the program then the compiler will provide a free copy assignment operator which again like the free copy Constructor will again just do a bitwise copy without considering what specific requirements the copy may have so it is always advised that like the Constructor and Destructor you should also provide the copy Constructor and the copy assignment operator whenever you are designing a class where copies are possible or where objects are likely to be passed to functions in call by value and in specific terms we have also seen here the Notions of deep and shallow copy with pointers please remember shallow copy will just copy the pointer so that after a shallow copy more than one pointer points to the same object and deep copy does not copy the pointer it copies the pointed object therefore after deep copy the two pointers point to two different copies of the possibly the originally same object but they become different uh objects so deep copy and shallow copy will have to be used naturally judiciously certainly if it is not required we will not try to do deep copy because it will involve the copying of the pointed data which may be costly because that will again need copy construction by recursive logic but in terms of safety using deep copy is often more safe compared to using shallow copy