const
Member Functions
You noticed that some of the member functions in
Cow
are labeledconst
. Those functions are highlighted incow.hpp
below.
cow.hpp
:
...
class Cow {
public:
// We can only have a Cow if we know
// how many spots it has and how old it is
Cow(int numSpots, int age);
Cow() = delete;
// Moo the right number of times.
void moo(int numMoos) const;
// Accessor member functions
int getNumSpots() const;
int getAge() const;
// Mutator member functions
void setNumSpots(int numSpots);
void setAge(int age);
private:
// Per-Cow data
int numSpots_;
int age_;
};
...
Yeah, so I don't know what it means for a function to be
const
.Do you remember what it means for a variable to be
const
?Sure, it means that the variable's value can't be changed. The variable is, like, read-only.
It's a very similar idea, but it's the object that the member function is called on that is read-only.
A const
member function makes a promise not to change anything about the object it is called on. Specifically, it promises not to change the values of the member variables.
Basically, inside a const
member function, the object itself is considered const
. More to the point, its member variables are considered const
.
Note that the
const
keyword is part of the function signature, so it appears both in the declaration (in the.hpp
file) and in the definition (in the.cpp
file).We'll see later in the semester that it is possible to have two different functions with the same name, but one is
const
and the other isn't.So, whether or not a function is
const
is part of that function's identity!
cow.cpp
:
...
using namespace std;
Cow::Cow(int numSpots, int age)
: numSpots_{numSpots},
age_{age}
{
cout << "Made a cow with " << numSpots_ << " spots!" << endl;
}
void Cow::moo(int numMoos) const {
for (int i = 0; i < numMoos; ++i) {
cout << "Moo! ";
}
cout << endl;
}
int Cow::getNumSpots() const {
return numSpots_;
}
int Cow::getAge() const {
return age_;
}
void Cow::setNumSpots(int numSpots) {
numSpots_ = numSpots;
}
void Cow::setAge(int age) {
age_ = age;
}
Notice that none of the
const
member functions change the values of any member variables.
Why Use const
Member Functions?
Why would we want to make a
const
member function? What are they for?Good question! There are a few reasons to use
const
member functions.
- Automated correctness checking: If you don't intend for a function to change any member variables, making it
const
will allow the compiler to check that for you! - Interface contract: Anyone using your class will know that they can call a
const
member function and nothing will change about the object without having to check the actual code! - See below…
Also,
const
member functions have a special meaning forconst
variables that store an object. Check outmain.cpp
below!
main.cpp
:
...
using namespace std;
int main() {
Cow bessie{3, 12};
const Cow mabel{1, 2};
// This line wouldn't work!
// Cow duke;
bessie.moo(1);
mabel.moo(2);
bessie.setAge(4);
// This line wouldn't work!
// mabel.setAge(2);
return 0;
}
In this example, the variable
mabel
is declared asconst
. What do you suppose that means?I know! It means that you can't reassign
mabel
to a differentCow
object!That's true. But maybe that's not quite enough. Maybe we can't reassign
mabel
but what if we could use the setter methods to change everything aboutmabel
? That feels like a sneaky way to get around theconst
qualifier.Oh, yeah, that would be bad. What do we do?
To avoid that, there is a rule against changing the member variables of a
const
object.
More Reasons to Use const
Member Functions
- Allowing constant objects: If you have a
const
object, you can only callconst
member functions on it. That way aconst
object really is constant and cannot be changed.
If your class has no
const
member functions, then instances of your class will not work properly with theconst
qualifier!In general, any member function that can be
const
should beconst
.
Check-Up Questions
Okay, but what would it mean if we put
const
at the front?Good question, let's ask everyone else!
(When logged in, completion status appears here.)