Hello, there.
I'm going to start off this advanced C++ course by going over some background material.
So this is to make sure that everyone understands the basics before we go onto the more advanced material.
We're going to start off with local variables and function arguments.
When we define a local variable, it's inside a scope, which means a pair of curly brackets or braces.
So that's the start of the scope.
That's the end of the scope.
We define the variable inside the scope and then after that we can use it.
So when the program reaches this point, it'll allocate memory on the program's stack.
In this case, enough memory to hold an int, and it'll initialize it.
If we give it an initial value, it'll use that.
Otherwise, it'll use a default value. For a built-in type like int, it will use whatever data happens to
be in the memory already.
If it's an object of a class, it'll use the class's default constructor to initialize the object.
And then when the program reaches the end of the scope, this variable will be destroyed, which
means that the memory it was using will be released and the data in it is no longer accessible to the
program - or to the programmer, anyway.
So we say that i has gone out of scope or is not in scope or is no longer in scope.
When we pass a variable to a function, if we don't do anything special, then this is going to be passed
by value.
So this means that there'll be a local value, y, inside this function; it'll be initialized from
whatever variable we pass into it.
So in this case, y is going to be an int with the same value as x, which is two. The return statement
means that we copy y into the function's return value.
So the function's return value will [be] two in this case.
And then y gets destroyed at the end of the function. When the function returns,
we can then copy the data from this returned value into another variable, and in this case, we're
using it to initialize a new variable, z.
So let's try this out.
Here's a main function where we define the variaable x, then we call the function, passing x as its argument,
and then we return y from the function, and we use that to initialize z. I'm going to print out the
address of all these variables, using the address-of operator, so we can see if they're the same variable
or different variables.
So let's try that.
So there we are, x, y and z all have different addresses, which means they're different objects.
And then x still has the value two after we call the function and now z has the value two.
Because these are all different variables, if we change the value of y it won't actually affect
the value of x.
And let's try that.
So there we are.
x is
still two and z it will be the return value from this function, which is now.
one.
If we want to change the value of x, there's actually two ways we can do this.
One is pass by address.
So in this case, instead of passing the variable, which will cause it to be copied, we pass the address
of the variable.
And then the function needs to take a pointer.
So this will now have a local variable y, which is a pointer to int, and the address is going
to be the address of x.
And then if we dereference that, that'll give us the data in that memory, which is also used by
the variable x.
So if we change that data to one, then x will have the value one.
So here's the code again.
I've now changed it, so we're passing the address of x and we're taking a pointer in func.
So we now dereference y, so that equals one.
So let's have a look at what we get.
So the address of x is the same as the value of y, so y is a pointer.
So they're both pointing to the same bit of memory.
z is a different value, again.
So z is equal to the return value, which is one, but because we've changed the value of x through
this pointer, the value of X is now also one.
And then the other way to change the address of x is to use pass by reference.
So this means that the function now takes a reference instead of a pointer.
So the local variable y in here is going to be a reference to x.
So this means that y can access x's data.
Usually, this is implemented as a pointer which automatically gets dereferenced by the compiler.
So in effect, it's a simpler way of writing what we had before.
So if we make these changes, so we now using a reference.
And in fact, x and y have the same address.
So in this case, the compiler has implemented the reference as a pointer to x. And we can see this
x is one when the future returns.
Then finally, one useful thing you can do is pass by const reference.
So this means that the thing that's being referred to can't be changed.
(So I just move that out of the way.)
(Down here now!)
So if you have a class object and your function only wants to inspect the object, so that's read-only
access, then usually the most efficient way of doing that is to pass it by reference to const.
Because you're not passing by value, that means you're not making a copy, so there isn't any overhead
from copying the data, which can be quite expensive for some classes; it can involve a lot of operations
which add to execution time. And because the reference is to a const,
this means that this object here can't in fact be changed.
So, in here, let's change this to const.
So we get an error, which says you cannot assign to a variable that is constant.
And that's because y is a reference to const.
So we're not allowed to use y to modify x.
And then if we remove that line, it does work.
Okay, so as I say, it's all pretty basic stuff.
Nothing in here should be a surprise, I hope. But it's useful to remind ourselves and perhaps also
to get a better understanding of how these things work.
Okay, so that's it for this video.
I'll see you next time.
But meanwhile, keep coding!