Home > Blogs > 30 tips in 30 days: Tip# 29: use the mutable storage type to hide implementation details

30 tips in 30 days: Tip# 29: use the mutable storage type to hide implementation details

Danny Kalev

Danny KalevPosted December 25, 2007

Topics: Programming, C/C++

The mutable storage specifier denotes a data member that is never const, even if its object is const-qualified. As such, mutable members are particularly useful for hiding implementation details from a client that calls a const member function.

const member functions do not change the state of their object. However, in some cases you still need to modify data members inside a const member function -- without altering the observable state of the object. Such members are usually internal flags and other private bookkeeping variables that are not exposed to external clients. Let's look a concrete example. Class std::string represents its string as a char* member that points to an array of characters. Yet unlike traditional C-strings, the std::string char array doesn't need to append a '\0' terminator at its end because the length of the string is available by calling length() or size(). There is one exception to this rule: when you call string::c_str(). c_str() returns a const char * that points to a '\0' terminated char array. The most efficient way to satisfy this requirement is to have c_str() append the '\0' character. However, c_str() is a const member function. It cannot modify any data members of its string object -- unless they are declared mutable.

By declaring the string's char* member as mutable, the implementer of std::string can defer the appending of the '\0' character to c_str() member function. This way, not only does the implementation of class std::string become more efficient since it appends '\0' only when it must, but the string implementer also hides implementation details (e.g., at what stage exactly is the '\0' appended) from the clients.

Comments

Would using const_cast to achieve the same effect just be another way to skin a cat, or would there be any other important differences that I may be missing?

By howards.wastebasket, Aug 31, 2008 12:30 AM

Very useful information, thanks.

By sdafforn, Sep 9, 2008 10:47 AM

It can be viewed at http://www.informit.com/blogs/blog.aspx?uk=more-about-mutable. Due to technical difficulties he was not able to reply directly to this original thread. We apologize for the inconvenience.

By Dustin Sullivan, Sep 22, 2008 11:56 AM

std::string::c_str() is not a good example of when to use mutable. A class with a char* member variable cannot modify that member in a const member function, but because const is shallow it can modify the characters the member points to. So in the case you describe, mutable is not necessary to write a '\0' to the end of the string.

By informit1997, Aug 11, 2009 08:54 AM

Log in to comment

Become an InformIT Member

Take advantage of special member promotions, everyday discounts, quick access to saved content, and more! Join Today.

Informit Network