Skip to content

Instantly share code, notes, and snippets.

@kkabdol
Last active October 22, 2016 08:49
Show Gist options
  • Select an option

  • Save kkabdol/14d239d9471aafe4bed844555a8351b9 to your computer and use it in GitHub Desktop.

Select an option

Save kkabdol/14d239d9471aafe4bed844555a8351b9 to your computer and use it in GitHub Desktop.
mutable keyword
#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