So in the previous discussion of the prototype design patterns we saw that using operator equals for
assignments like we did here is insufficient when it comes to pointers because essentially the problem
is that we have a pointer here and the pointer just gets its value copied.
Which isn't a particularly good thing.
So we want to somehow be able to perform a DB copy so this operator equals here we can call it a shallow
copy.
And we want some sort of deep copy and the question is Well how exactly are you prepared to implement
the copying.
Do you want this special interface or do you just want to use some of the ordinary parts of C++ in order
to facilitate the copying of a particular object so one of the ways in which we can do this is by defining
a copy constructor so a copy constructor would take another contact as its argument and would simply
attempt to replicate all the members of the original.
So the way this can work is something like the following so if I make a contact which takes another
contact by reference I'm going to call it other than what you can do here is you can just take there
and try to copy all of its members.
So essentially when it comes to the name for example you can take the name.
And similarly what you can do is for the address we know that we cannot simply take other address because
that simply copies the pointer.
So what you have to do here is perhaps construct a new address where you take other daughter address
and take the different members so you take the street you take the city and you take the sweet as well
so that address this week like so.
So this is a very explicit way of actually replicating the entire address just by copying every single
field that you have there and this approach will actually work.
So what you would do typically is you would say I'm going to have contact Jane which is going to be
initialized from John so I'm going to put the curly braces in here and having done this we can actually
run this and we're going to get the correct result surprisingly enough because what's happening is a
deep copy rather than a shallow copy.
So as you can see the suite numbers here are different.
There's one two three here and 1 0 3 here.
So this is one approach that can work.
Another approach is if you want to have this operation performed recursively and if you want a more
robust implementation instead of having it like this what you do is you also give a address it's own
copy constructor as well.
So essentially what we can do is we can go into a address here and we can simply implement a very similar
structure.
So you would have an address which takes Konst address a reference call the address.
And here you would perform the same assignments.
So essentially you would assign the street to address door suite and you would assign the city to address
dot city and you would assign the city to address those suite.
And this way what you can do is you can simplify the code that is written down here because instead
of calling all of this all you have to do is you have to initialize the address in this case with a
different address.
Now this is tricky because essentially you still have this idea of a pointer and the copy constructor
takes a reference.
So what you would have to do here is you would have to still construct a new address.
But here ask the argument what you can pass as you can past that existing address so you can say there
Daut address but remember other address is a pointer.
So you have to dereference that pointer.
And that way you use the address copy constructor to basically replicate this in dress.
Let's actually try this as well let's just compile it and see it work hopefully hopefully see work with
see what's going on.
OK so element 1 conversion from Konst and char requies an hour and conversion.
Let's see.
Oh yeah.
We are using the wrong field here.
So it should be addressed.
ST.
There we go.
OK so hopefully this time round we actually get the correct result.
OK so it executes and once again the operation is correct so we essentially have a prototype.
So the original contact the contact Joe on here is acting as a prototype and then we replicate the prototype
by.
In this case using a copy constructor and then we customize the resulting instance so we set the different
name as well as a part of the address to something different as well.
So this is a very simple implementation of the prototype design pattern.