mirror of
https://github.com/VictorEijkhout/TheArtOfHPC_vol3_cppf08programming.git
synced 2026-01-24 22:44:48 +09:00
46 lines
1004 B
C++
46 lines
1004 B
C++
/****************************************************************
|
|
****
|
|
**** This file belongs with the course
|
|
**** Introduction to Scientific Programming in C++/Fortran2003
|
|
**** copyright 2016-2023 Victor Eijkhout eijkhout@tacc.utexas.edu
|
|
****
|
|
**** mutable.cxx : mutable const members
|
|
****
|
|
****************************************************************/
|
|
|
|
#include <iostream>
|
|
using std::cout;
|
|
#include <optional>
|
|
using std::optional;
|
|
|
|
class complicated {
|
|
public:
|
|
complicated(int n) {
|
|
cout << "making complicated thing\n";
|
|
};
|
|
};
|
|
|
|
//codesnippet withmutable
|
|
class has_stuff {
|
|
private:
|
|
mutable optional<complicated> thing = {};
|
|
public:
|
|
const complicated& get_thing() const {
|
|
if ( not thing.has_value() )
|
|
thing = complicated(5);
|
|
else cout << "thing already there\n";
|
|
return thing.value();
|
|
};
|
|
};
|
|
//codesnippet end
|
|
|
|
int main() {
|
|
|
|
has_stuff mystuff;
|
|
auto mything = mystuff.get_thing();
|
|
mything = mystuff.get_thing();
|
|
mything = mystuff.get_thing();
|
|
|
|
return 0;
|
|
}
|