ProgrammingHardIBM · Deloitte pattern

Which member function fails to compile?

Which member function fails to compile?
class C {
    int a = 0;
    mutable int hits = 0;
public:
    int get() const     { hits++; return a; }
    void set(int v) const { a = v; }
    void reset()        { a = 0; hits = 0; }
    int peek() const    { return a + hits; }
};
  1. A. reset
  2. B. get
  3. C. set
  4. D. peek

Correct answer: C. set

A const member function treats the object as const, so it cannot assign to ordinary members: set fails. get may change hits only because hits is mutable. reset is not const, and peek only reads.

More OOP with C++ and Java questions

Exams that ask this topic