OK, everybody, so this is going to be an extra lecture here on smart pointers.
In the last lecture, we went over a smart pointer called Unique Pointer, but there was some feedback
on the course that people really want to know about the other smart pointers.
I wasn't going to include them because I thought it was just something more specific to learning C++
itself.
Less of a general programming thing.
But I will go ahead and go over it because it seems like, you know, it might be useful and there's
interest in it, so I'm going to go over to other pointers, one called shared pointer and one called
weak pointers.
So those are the two other pointers.
There's three in total unique shared and weak smart pointers.
So I've made a little program here.
It's called Speed CBP and I've put some stuff in here.
We have a class that's just called something.
You notice that it has a public constructor and destructor, as well as a little method or member function
here called Get Thing, which just prints out this one private data member, right?
So what we've done so far is just make a unique pointer like this right and type something, and we
can just call this, you know, like you P1 and we've done something like this.
So this will just make a unique pointer, right?
And if I go, what I can do
is like, let's say I make two of these right like you P1 and you P2.
And what we talked about was when this when these pointers go out of scope, which is here in the function,
right?
Once it gets past this bracket returns zero and goes past year.
That's when these two go out of scope and they automatically will d allocate themselves.
They will automatically call the destructor right.
And here is the destructor, and it prints out the allocated something.
So we should see this printout twice, right?
So if I go back here, I compile this and it's just going to be out, I noticed this SD allocated something
twice, right?
So that's what we've been doing so far.
So another thing that I'd like to point out is that there is another way to make these pointers first.
So what you can actually do is you don't have to use this syntax right here.
What you can instead do is like you P2 equals and then this is a special kind of constructor constructor
here.
It's actually called make unique and you just say type something right here and you can put some little
parentheses like that.
This is kind of a preferred way to do it.
These will both work.
The only thing is that when you use this special kind of way of allocating the memory for a unique pointer,
it actually uses something out of this code base here, and it can deal with exceptions.
So we've in this course, we talk a little bit about exceptions.
If you do it like this, you may run into some issues if you are dealing, with some exceptions to do
with the pointer.
So this is kind of known as a better syntax to use to make it, although this is totally acceptable
most of the time too as well.
So I just want to point that out, and we will be needing to use this style of syntax for the other
smart pointer.
So let's get into the next type of smart pointer.
So the thing about the unique pointer that we mainly talked about is it just goes out of scope and it's
basically done right.
It makes a new piece of memory.
And then once it goes out of scope, it's totally done.
So a question that would lead us to talk about the next type of smart pointers.
What if we wanted to do something like this?
We just wanted to make another pointer, right?
Well, like, hey, we have a smart pointer that's pointing to, you know, up one right?
Or it's pointing to a new something.
Let's call up one.
And what if I just want to make another pointer, right?
Well, this is pointing to the same piece of memory once this goes out of scope, basically, you know,
this one is automatically deleted.
So and you don't actually, this is going to be a problem.
You won't be able to actually do this even right here because it's not really meant for this type of
thing.
It's only meant to be like a unique standalone point of view, hence why it's called unique pointer.
So what if I try and compile this right here?
So it actually says no, it has an error and it says like use of deleted function.
And that's because if you look at the code base of unique pointer where it's written and implemented,
it actually does, you know, just delete it so you can't actually use this anymore.
It's just done.
So you cannot make a copy with the assignment operator right here.
So.
What do we do if we want to do something like this, we are not going to use a unique pointer, but
what we are going to use is something called a shared a pointer because we're going to share the same
region of memory, right?
So we can make a shared pointer call up to equals update one.
So what we really should do is instead, I make like a shared pointer right here.
But what I'm going to do is I'm going to instead make make it using that different style, right?
That different style of allocating.
So I'm going to say, firstly, make shared instead of make unique and I'm going to say something just
like that.
And what I can do is I'll just say ESP two equals ESP one.
So this is the type of thing I would want to do if I want to make like another pointer pointing to the
same piece of memory.
And this is how I would want to initialize it in the beginning, right, if I make the initial shared
or one, I would want to use this special part of coming from the memory code base there and say, make
shared.
So what's cool about this, though?
Like what's the why would we use this?
Well, of course we talked about this, but another really cool thing is that once this shared pointer
SP1 one goes out of scope.
It doesn't necessarily allocate the object.
Right.
So let me just let me show you kind of like what's going on here.
So this only makes one piece of memory, right?
So if I go ahead and run this right now?
Compile it and run it.
It only says the allocated something once.
And that's because this is the only real allocated memory.
This is just another pointer pointing to the same memory.
The really cool thing, though, is that once SP1 goes out of scope, it doesn't delete it right then.
Right.
So if I weren't right here, I'm just going to put some sort of like, you know, scope block.
So if I put something in here, let's just say I make another thing here, so we'll have a shared pointer
SP1 here.
Actually, I'm going to put this in here.
I'm going to have another thing up here.
So if I say shared pointer something and I'll just call this like s
and what I will do is, I will say s equals one.
Right here, so I'm declaring one up here, right, and then I make one here and I'm pointing this,
I'm basically copying like, I'm making another pointer.
This and this is right here is pointing to the same region of memory as this right here.
This spun right.
So shared another shared pointer.
This shared pointer, they all share this one like control block is what it is like a one piece of memory,
right?
And it keeps track of how many pointers you have pointing to that memory.
It's like special in that sense that it keeps track of all the references.
It has a reference count.
How many pointers are referring to this one control block of memory?
In this case, there's two, right.
We have one right here pointing to it, and then we have this one pointing to the same block of memory,
right?
This is the really cool thing is that this was created in this scope of this.
If, right, once this goes out of scope, you would think if it was like unique pointer, it would
delete it and we would no longer have access to that region of memory.
So we wouldn't be able to do something, for example, like s get thing right.
So what if I wanted to, you know, that's going to print out the thing.
So.
You would think that I wouldn't be able to do this right because this memory would be gone.
Well, it's actually totally fine.
So I'll show you it'll go ahead and print it out.
So if I compile this and I run it, you know, there was no compiler errors there and noticed that it
printed it out and then it dislocated it, right?
And so what it was really doing is it waited until this one went out of scope, right?
This is the last reference pointing to that region of memory.
So it waited for us to go out of scope here.
That's why we saw this thing is zero originally right?
And it printed out here.
So we saw it print out here before the allocated it right was the allocated.
Something happened after that.
You know, if we go here, you notice that zero printed out and then reallocated something printed out.
So that means that the SE did not get the allocated until down here.
So what it does is that the shared pointer keeps track of all of the references and the reference count,
and it's not going to delete the region of memory.
It's not going to allocate it until all of the reference cat like the very last pointer pointing to
it goes out of scope.
That's when it's going to be allocated.
So that's what's cool about the SharePoint.
Like, we can make a lot of things pointing to the same region of memory, and we don't have to like,
rely on being in the same scope and having that memory like it deleted, you know, like a unique pointer
would have totally gone out of there.
The problem is we can't we weren't even able to assign, you know, another thing to the unique pointer.
So we couldn't even test that out if I wanted to do the same thing with unique pointer because I cannot
like make something else pointing to the same region of memory since it's unique.
So that is a really cool thing about the shared pointer, and I can make as many references as I want
to to this one region of memory that was originally, you know, SP1 was pointing to it originally.
I can make a bunch more shared pointers pointing to the same thing, and they could all be at their
own levels of scope.
And I wouldn't have to worry about the, you know, memory going being allocated before these other
pointers that I made going or going out of scope.
You know, if I want to still access them.
So there is one more type of pointer and that is a weak pointer.
So the weak pointer is similar to the shared pointer, except it doesn't add another reference count
like noticed that this thing added another reference count to the shared pointer, right?
It has this control block, and it's keeping track of all the references.
How many references are there?
A weak pointer would be something that I guess you would only want to use temporarily because it wouldn't
be considered, as you know, an extra reference count.
So if I made this like a weak pointer, so I make this weak point s, then it's not going to be considered
reference count.
So when this goes out of scope, it'll totally be out of scope.