Last active
October 22, 2016 08:49
-
-
Save kkabdol/14d239d9471aafe4bed844555a8351b9 to your computer and use it in GitHub Desktop.
mutable keyword
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using std::cout; | |
| using std::endl; | |
| static int SomeDataSource = 100; | |
| class LazyFetching | |
| { | |
| public: | |
| LazyFetching() : m_pDataSource( nullptr ) {} | |
| int GetData() const; | |
| private: | |
| mutable int* m_pDataSource; | |
| }; | |
| int LazyFetching::GetData() const | |
| { | |
| if( m_pDataSource == nullptr ) | |
| { | |
| m_pDataSource = &SomeDataSource; | |
| } | |
| return *m_pDataSource; | |
| } | |
| int main() | |
| { | |
| LazyFetching obj; | |
| cout << obj.GetData() << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment