# Lecture 12

Recall const obj, const methods

```
struct Student {
  int assns, mt, final; 
  float grade() const; //doesn't modify fields, so declare it const
}
```

Now consider: We want to collect usage stats on student objects

```
struct Student {
  ...
  int numMethodCalls = 0;
  
  float grade() const { 
    ++numMethodCalls;
    return ... ;
  }
}
```

If you remove const from grade(), then you cannot call grade on const Students, but mutating numMethodCalls affects only the physical constness of student objects, not logical constness.

We want to be able to update numMethodCalls even if the object is const. **ANSWER:** Declare **field mutable**

```
struct Student {
  ...
  mutable int numMethodCalls = 0;
  float grade const() {
    ++numMethodCalls;
    return ...;
  }
}
```

## Static Fields and Methods

* numMethodCalls tracked number of times method was called on for particular object.

What if we want to track the number of times a method is called over all student objs? Or what if we want to track how many students are created? **ANSWER**: Static members!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lauradang.gitbook.io/notes/c++/cs_246/lecture-12.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
