In RAII, resources are not initialized until they are available. However, many access methods are declared persistent. I need to call a function mutable(not const) to initialize a data item.
mutable
Example: booting from a database
struct MyClass { int get_value(void) const; private: void load_from_database(void); // Loads the data member from database. int m_value; }; int MyClass :: get_value(void) const { static bool value_initialized(false); if (!value_initialized) { // The compiler complains about this call because // the method is non-const and called from a const // method. load_from_database(); } return m_value; }
My primitive solution is to declare the data item as mutable. I would prefer not to do this because it assumes that other methods can modify a member.
How can I get the operator to load_from_database()get rid of compiler errors?
load_from_database()
RAII. RAII , .
, Lazy. .
Lazy
mutable, .
, const_cast, , - :
const_cast
static const MyClass Examplar;
, ? , const_cast undefined. .
const_cast, R Samuel Klatchko do.
R Samuel Klatchko
, , . , : get, set load_from_database, , mutable.
get
set
load_from_database
. , , .
, , , ( ) RAII. , , const mutable . const mutable .
const
, , , , - . ++ ( ), - , (, , ) . (.. -, ).
( ) MyClass, , - , . : 1) MyClass - lazy_int 2) ( , ) get_value(), , operator int(). , m_value, , , , , .
MyClass
lazy_int
get_value()
operator int()
m_value
. int ( ) operator int(), , .
[ ! !:))]
struct DBValue { int get_value(); private: void load_from_database(); int value; }; struct MyClass { MyClass(): db_value(new DBValue()) {} ~MyClass() { delete db_value; } int get_value() const; private: DBValue * const db_value; }; int MyClass::get_value() const { return db_value->get_value(); // calls void load_from_database() if needed }
, MyClass const , , const non const const .
const_cast, . mutable , , , , , , , .
If your method changes the state of the object (for example, changing the state of the base database), then the method should not be const. In this case, you must have a separate, non-constant loadmethod, which must be called before the call const.
load
This method requires neither const_castnot mutable, and will make a potentially expensive operation explicit.
Source: https://habr.com/ru/post/1737709/More articles:Можно ли отобразить в табличной форме с помощьюив html? - jsonWIF using SAML 2 / Federate AD FS 2.0 with CAS - wifhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1737706/bind-different-entries-to-datagridviewcomboboxcell-from-a-data-source&usg=ALkJrhj4i4i41CvoNbBhcWbtm-ufZYHMYwHibernate Convert HQL API to criteria - nhibernatehttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1737708/implement-collision-detection-by-extendung-the-rectangle-class-to-use-its-contains-and-intersects-methods&usg=ALkJrhhiLlb3RLn3gc89LpKYZyE-fMk3bAhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1737710/what-are-some-arguments-to-support-the-position-that-the-dojo-javasscript-library-is-secure-accessible-and-performant&usg=ALkJrhjjXe0d0H9OdPrpqyIRYPxUC18n9QПоддержка протокола IMAP на разных почтовых серверах - imapEDM -> POCO -> WCF (.NET4). But passing the collection calls IsReadOnly set to TRUE - entity-frameworkto catch an event in VBScript - domTesting custom rail methods - ruby-on-railsAll Articles