CS 70

const Member Functions

  • LHS Cow speaking

    You noticed that some of the member functions in Cow are labeled const. Those functions are highlighted in cow.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_;
};
...
  • Cat speaking

    Yeah, so I don't know what it means for a function to be const.

  • LHS Cow speaking

    Do you remember what it means for a variable to be const?

  • Cat speaking

    Sure, it means that the variable's value can't be changed. The variable is, like, read-only.

  • LHS Cow speaking

    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.

  • RHS Cow speaking

    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).

  • LHS Cow speaking

    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.

  • RHS Cow speaking

    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;
}
  • LHS Cow speaking

    Notice that none of the const member functions change the values of any member variables.

Why Use const Member Functions?

  • Duck speaking

    Why would we want to make a const member function? What are they for?

  • LHS Cow speaking

    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…
  • RHS Cow speaking

    Also, const member functions have a special meaning for const variables that store an object. Check out main.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;
}
  • LHS Cow speaking

    In this example, the variable mabel is declared as const. What do you suppose that means?

  • Dog speaking

    I know! It means that you can't reassign mabel to a different Cowobject!

  • LHS Cow speaking

    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 about mabel? That feels like a sneaky way to get around the const qualifier.

  • Cat speaking

    Oh, yeah, that would be bad. What do we do?

  • LHS Cow speaking

    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 call const member functions on it. That way a const object really is constant and cannot be changed.
  • RHS Cow speaking

    If your class has no const member functions, then instances of your class will not work properly with the const qualifier!

  • LHS Cow speaking

    In general, any member function that can be const should be const.

Check-Up Questions

Why mark a member function as const?

Where do we put the const when we declare a member function const?

  • Duck speaking

    Okay, but what would it mean if we put const at the front?

  • LHS Cow speaking

    Good question, let's ask everyone else!

What would it mean if we put const at the front of a member function declaration?

(When logged in, completion status appears here.)