Lecture thumbnail 0:00 / 0:00 I think we can all agree that having the clonable interface for copying objects is not the way to go,

quite simply because it’s badly specified because it returns an object instead of something more strongly

typed.

And in general, we don’t know what to do with it and neither does the user of the object.

Even if you specify the documentation which says this is a deep copy, it’s still uncertain whether

people would actually use it correctly.

So how about we avoid all this?

And there are different ways of actually copying your objects depending on how you want to work with

them.

And one approach, which is also not very clear, but at least somewhat better, is using copy constructors.

Now a copy constructor is a C plus plus term.

It comes from the C plus plus programming language, and it basically lets you specify an object to

copy all the data from.

So that’s what we’re going to do.

We’re going to get rid of this whole clone business and we’re going to take a look at how to actually

copy objects in the constructor.

So the idea is quite simple.

You make a special constructor, so you make a constructor which makes a person, but it makes it from

some other person.

I’ll call this other.

So the idea is that when you’re in person, what you do is you make a copy of other by copying its internals

into yourself.

So here we have two things we have names and address.

So in this case, we would say something like the following.

We would say names equals other names and we would say, Well, that should be a capital N and we would

say address equals other dot address.

And of course this isn’t sufficient.

This quite simply isn’t enough, because remember, address right now is being copied through shallow

copy.

So address needs a copy constructor as well.

Meaning you would call a new address with other dot address and we haven’t got such a constructor yet,

so maybe we can create one.

So here we have a copy constructor for the address as well.

And here we simply say street name equals other address, dot street name.

And similarly we have house number equals other address dot house number.

I specifically wanted it to be other, not other address.

Let’s rename it to other.

There we go.

Okay, so now we have a bunch of copy constructors.

We can get rid of all this stuff that we had in relation to the Clonable object.

So I will get rid of everything that’s clonable both here.

Let’s just do a search for clone to make sure that we haven’t missed anything.

So I’ll get rid of this I clonable specification and we can now work with this new API.

So here is how you would do it.

You would say var Jane equals new person providing John.

So this is simply using a constructor to in effect initialize or instantiate or however you want to

call it.

So we basically, when we call this constructor with a pre constructed object, we copy all of its properties

and we do it recursively, which means we traverse the entire tree in order to actually go through the

elements and have them in Jane and then Jane can have a different house number.

And if we actually go ahead and we execute this, you can see that everything is working correctly.

We have three, two, one here and one, two, three here.

Of course, I could have updated the name as well, but it doesn’t really matter.

The point is this approach works.

It’s also not very idiomatic because copy constructors are a C plus plus thing and not many people would

recognize them in the kind of net and c sharp setting.

Play Play Stop Play Play Play Start Play information alert