diff --git a/section2/const.cpp b/section2/const.cpp index 51dd9e3..7ac7f6d 100644 --- a/section2/const.cpp +++ b/section2/const.cpp @@ -65,8 +65,50 @@ class DemoClass final { m_mutex++; } + void foo(){} + void bar() const { + // m_value++; + } + int get_mutex() const { + return m_mutex; + } +public: + DemoClass():m_value(0), m_mutex(0){} }; +// function calls of const instance +void case3() { + using namespace std; + DemoClass demo; + demo.get_value(); + demo.save_data(); + demo.foo(); + demo.bar(); + cout << "demo's m_mutex: " << demo.get_mutex() << '\n'; + + const DemoClass demo1; + demo1.save_data(); + demo1.get_value(); + // demo.foo(); + demo1.bar(); + demo1.get_mutex(); + cout << "demo1's m_mutex: " << demo1.get_mutex() << '\n'; + + const DemoClass &demo2 = DemoClass(); + demo2.save_data(); + demo2.get_value(); + // demo2.foo(); + demo2.bar(); + cout << "demo2's m_mutex: " << demo2.get_mutex() << '\n'; + + const DemoClass* demo3 = &demo; + demo3->get_value(); + demo3->save_data(); + // demo3->foo(); + demo3->bar(); + cout << "demo3's m_mutex: " << demo3->get_mutex() << '\n'; +} + constexpr int fib(int n) @@ -84,6 +126,7 @@ int main() case1(); case2(); + case3(); constexpr int fib5 = fib(5);