answersLogoWhite

0

What is mutable member?

Updated: 12/12/2022
User Avatar

Wiki User

11y ago

Best Answer

A variable that may be modified.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is mutable member?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is a constant member function c plus plus?

A constant member function is an instance method that can only modify the mutable members of the class instance. This is achieved by implicitly declaring the this pointer constant. The this pointer is a hidden parameter that is implicitly passed to every instance function, and that points to the current instance of the class. Static member functions do not have an implicit this pointer and therefore cannot be declared const. Consider the following simple class that has both mutable and non-mutable member variables, and both constant and nonconstant member functions: struct foo { void constant()const; void nonconstant(); private: int data1; mutable int data2; }; void foo::constant()const { data1++; // not permitted: the implicit this pointer is declared const data2++; // ok: data2 is mutable } void foo::nonconstant() { data1++; // ok: the implicit this pointer is declared non-const data2++; // ok: data2 is mutable } Note that data2 can always be modified since it is declared mutable, but data1 can only be modified by nonconstant member functions. Members should only be declared mutable when they are only used internally by a class (such as when locking a mutex for thread safety), or where a value needs to be calculated and cached the first time it is accessed.


In c plus plus what is Mutable keyword?

mutable keyword can only be applied to non-static and non-constant member of a class. It indicates that the corresponding data member can be modified even from the constant function. (Constant function: is a function marked with the keyword const) For eg. class x { private: mutable int query_count; int value; public: int get_value() const { query_count++; return value; } }; In the code above member query_count is marked with keyword mutable. Hence it's value can be modified from constant function get_value().


Are Wrapper classes immutable or mutable?

In java.lang all Wrapper classes are immutable .


What is mutable in java?

It means that something can be changed after it is created. Immutable means that it can't be changed.


What makes a parameter a constant reference?

In C++, the const modifier makes a reference a constant reference. int i = 42; const int& r = i; In the above example, both r and i refer to the same object: a primitive integral with the value 42. That is, r is an alias for i; they are one and the same object. However, the reference r is declared constant so we cannot change the object it refers to by assigning a new value to it. But we can assign a new value to i. And since r and i are the same object, the value of r also changes, despite being decalred constant. Normally you would not use a constant reference to refer to a local object, since the constness of the reference cannot be guaranteed. However, when we pass an object to a function that expects a constant reference to an object, we give a gurantee to the caller that the object's immutable members will not be changed by the function. This is because the function only has access to the object through the constant reference (formal argument), not the reference we actually passed (actual argument). Therefore it only has access to the constant member functions. Note that mutable members are nonstatic members that are declared mutable. Typically these will be declared private, for internal use only, such as counters or iterators. As such, modifying a mutable member does not alter an object's external state, as defined by its immutable members. This allows us to invoke constant member functions, which assures us that the object's external state remains unchanged, regardless of the internal state. Mutable members can also be used to enable lazy construction. That is, an accessor initialises a complex embedded member object when it is first accessed, rather than during construction. This allows the containing object to be constructed much faster, provided the accessor is not invoked as part of the construction process of course. In order for this to work, the embedded member must be a mutable pointer that is initially NULL. When the accessor is invoked for the first time, the complex member is constructed and assigned to the NULL pointer which is then returned (usually by constant reference, but not always). Thereafter, the pointer is no longer NULL and can simply be returned on subsequent accesses. The main advantage of lazy construction is that if the accessor is never called before the containing object falls from scope, we don't waste time and memory constructing an embedded object that was never actually required (and if we had a thousand containing objects, we'd be constructing a thousand embedded objects for no good reason). However, all accessors must be declared constant as they must be accessible to all other member functions, including constant member functions. If the member were not mutable, lazy construction of that member becomes impossible.

Related questions

What is a constant member function c plus plus?

A constant member function is an instance method that can only modify the mutable members of the class instance. This is achieved by implicitly declaring the this pointer constant. The this pointer is a hidden parameter that is implicitly passed to every instance function, and that points to the current instance of the class. Static member functions do not have an implicit this pointer and therefore cannot be declared const. Consider the following simple class that has both mutable and non-mutable member variables, and both constant and nonconstant member functions: struct foo { void constant()const; void nonconstant(); private: int data1; mutable int data2; }; void foo::constant()const { data1++; // not permitted: the implicit this pointer is declared const data2++; // ok: data2 is mutable } void foo::nonconstant() { data1++; // ok: the implicit this pointer is declared non-const data2++; // ok: data2 is mutable } Note that data2 can always be modified since it is declared mutable, but data1 can only be modified by nonconstant member functions. Members should only be declared mutable when they are only used internally by a class (such as when locking a mutex for thread safety), or where a value needs to be calculated and cached the first time it is accessed.


What is the Tagalog of mutable?

The Tagalog word for "mutable" is "maaring baguhin" or "mababago."


What are mutable classes?

All classes that we create are mutable class. No special technique is used. Don't get confused. Mutable is said to compare it with immutable classes. That's it.


What is a sentence using the word mutable?

the stock market experiences mutable money surpluses and losses.


How do you use mutable in a sentence?

Mutable is used to describe something that is liable to change. A sentence using the word could be "The personalities of young girls seem mutable depending on which group of peers they may be around."


In c plus plus what is Mutable keyword?

mutable keyword can only be applied to non-static and non-constant member of a class. It indicates that the corresponding data member can be modified even from the constant function. (Constant function: is a function marked with the keyword const) For eg. class x { private: mutable int query_count; int value; public: int get_value() const { query_count++; return value; } }; In the code above member query_count is marked with keyword mutable. Hence it's value can be modified from constant function get_value().


What are the zodiac elements?

Aries - Cardinal Fire Taurus - Fixed Earth Gemini - Mutable Air Cancer - Cardinal Water Leo - Fixed Fire Virgo - Mutable Earth Libra - Cardinal Air Scorpio - Fixed Water Sagittarius - Mutable Fire Capricorn - Cardinal Earth Aquarius - Fixed Air Pisces - Mutable Water


What is a synonym for the word variable?

inconstant, mutable


What are the release dates for Mutable Fire - 1984?

Mutable Fire - 1984 was released on: USA: 1984 (limited) Czech Republic: 14 September 2003 (CESTA Festival)


What mutable and immutable in java and example program?

lmutable object


Does an inconsistent person have mutable opinions?

No, because there are believable answers


Is Pisces earth fire water or air?

Pisces are Mutable Water.