const Member Functions
You noticed that some of the member functions in
Coware labeledconst. Those functions are highlighted incow.hppbelow.
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
constkeyword is part of the function signature, so it appears both in the declaration (in the.hppfile) and in the definition (in the.cppfile).
We'll see later in the semester that it is possible to have two different functions with the same name, but one is
constand the other isn't.
So, whether or not a function is
constis 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
constmember functions change the values of any member variables.
Why Use const Member Functions?
Why would we want to make a
constmember function? What are they for?
Good question! There are a few reasons to use
constmember functions.
- Automated correctness checking: If you don't intend for a function to change any member variables, making it
constwill allow the compiler to check that for you! - Interface contract: Anyone using your class will know that they can call a
constmember function and nothing will change about the object without having to check the actual code! - See below…
Also,
constmember functions have a special meaning forconstvariables that store an object. Check outmain.cppbelow!
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
mabelis declared asconst. What do you suppose that means?
I know! It means that you can't say
mabel = somethingElse;because that would change whatmabelis.
That's true. But maybe that's not quite enough. Maybe we can't reassign
mabelbut what if we could use the setter methods to change everything aboutmabel? That feels like a sneaky way to get around theconstqualifier.
Oh, yeah, that would be bad. What do we do?
To avoid that, there is a rule against changing the member variables of a
constobject.
More Reasons to Use const Member Functions
- Allowing constant objects: If you have a
constobject, you can only callconstmember functions on it. That way aconstobject really is constant and cannot be changed.
If your class has no
constmember functions, then instances of your class will not work properly with theconstqualifier!
In general, any member function that can be
constshould beconst.
Check-Up Questions
Okay, but what would it mean if we put
constat the front?
Good question, let's ask everyone else!
(When logged in, completion status appears here.)