Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add demo of function calls of const instance #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions section2/const.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -84,6 +126,7 @@ int main()

case1();
case2();
case3();

constexpr
int fib5 = fib(5);
Expand Down