- [Illustrator] Qualifiers are used to adjust qualities
of an object or a variable.
This is an example of a variable declaration
with qualifiers.
In this example, the const and static keywords
are qualifiers.
They tell the compiler that this variable will be immutable
that's the const qualifier,
and they will have static storage duration.
There are two types of qualifiers in C plus plus.
CV qualifiers and storage duration qualifiers.
CV stands for constant and volatile.
Const marks a variable as read only or immutable.
It's value cannot be changed once it's been defined.
Mutable is used on a data member
to make it writeable from a const qualified member function.
I'll cover this in more detail in the companion course,
C plus plus advanced topics.
And volatile marks a variable that may be changed
by another process.
This is rarely used and only be partly deprecated
in C plus plus 20.
It may be useful for external variables
under very special circumstances.
Storage duration qualifiers are used to define the duration
or lifetime of a variable.
By default a variable defined within a block
has automatic lifetime
where the lifetime of the variable
is the duration of the block.
There's no qualifier for this because it's the default.
There used to be an auto qualifier, but it was rarely used
so the keyword has been repurposed.
Variables declared as static,
have life beyond the execution of a block.
Static variables live for the duration of the program.
Static variables are commonly used for keeping state
between usages of a given function or method.
By default, a variable defined outside of any block
is static.
Register variables are stored in processor registers.
This can make them faster and easier
to access and operate on.
This qualifier is taken as a suggestion by the compiler.
The compiler may or may not actually store
the variable in a register.
An extern variables are defined
in a separate translation unit
and are linked with your code
by the linker step of the compilation process.
So let's take a look at a couple of these in code.
This is qualifiers dot CBP from chapter three
of the exercise files.
We have a class here that we're just using for an example,
it's very simple class that has one member function
or method.
We have a function here, again, a very small,
simple function that simply increments a variable.
And then we have main where we have defined a variable
and we printed out.
So if I run this, you'll see it says the integer's 42.
Now, if I want to change the value of this integer,
I can simply change it and when I build and run, you see,
it says, the integer is now 73,
but if I declare this variable as const,
now, if I try to build it, my build fails
and I get this error message cannot assign variable 'I'
with const-qualified type 'const int'.
So that's a lot of words to say, can't do that,
this variable is read only.
And so, you know, again,
if I remove that const qualifier,
now it builds and runs and I can change the value.
So that's the const qualifier we've used it before,
I think it's pretty straightforward.
Now, if I take this, 'I', and I sign it
the return value from the function,
you notice that the function declares a variable 'X'
and then increments it before returning it.
So it should return an eight instead of a seven.
So, it's seven incremented in, we should get an eight
so when I build and run, you see the integer is eight.
And what's interesting here is if I run this three times,
obviously I can't declare the variable three times,
but I can run it three times
and what's the value going to be?
Well, it gets incremented each time,
but this is an automatic variable.
It's not static.
So, the result will still be eight.
On the other hand, if I declare this as a static variable,
it'll now increment each time and it keeps its value
between executions of the function.
This variable is no longer automatic,
it's now stored in static memory space.
And each time you come into the function,
it retains its value from the last time.
So now we should get eight, nine, 10,
something like that, right?
And the integer is 10
because I've run the function three times
and I've incremented that value three times
and seven plus three is indeed 10.
So, this works in an unexpected way with a class.
So, what I'll do now is I'm going to declare a variable
from this class, I'm going to call it S1.
I'm going to actually declare three of them
and we'll call them S2 and S3.
And I'm going to print them all out
S1 dot static value, right?
S2 dot static value and S# dot static value.
And what's interesting here is now
these are all three different instances
of this class, right?
There are three different objects.
So what do you expect is going to happen?
What's going to happen is not what you would think.
I'm going to go ahead and I'm going to build this and run it
and you see that the integer is eight,
the integer is nine and the integer is 10.
Even though these are three different objects.
And the reason for that is that a static variable
inside a member function has global storage.
It is not stored with the object it's stored with the class.
And so, each instance of that class,
each object actually shares the same variable.
And so, this is a really important distinction.
If you're going to declare something static
inside a member function, keep in mind
that each instance of that class,
each separate object that is instantiated from that class,
all share the same variable.
Qualifiers are used to control the quality of variables.
Some of these like const and static you'll use frequently.
Others are good to know about for when you need them
or when you see them in code.