How to count the calls of a method that marked as const in C++

1 Answer

0 votes
#include <iostream>
 
class Example {
private:
    mutable int m_Count = 0; // mutable allows changing a variable in method const 
public:
    void Print() const {
        m_Count++;
        std::cout << "Print(), call number: " << m_Count << "\n";
    }
};
 

int main() {
    Example ex;
     
    ex.Print();
    ex.Print();
    ex.Print();
}
  
  
  
  
/*
run:
  
Print(), call number: 1
Print(), call number: 2
Print(), call number: 3
  
*/

 



answered Feb 2, 2023 by avibootz

Related questions

...