Thread: "Faking" const via mutable class member, opinions?

  1. #1
    Guest
    Guest

    "Faking" const via mutable class member, opinions?

    I'm dusted off a file-reading class I'd been working on earlier, which implements some comfort features and iterators. Since the class doesn't modify the file it's being given but only its own internal (book keeping) state, I decided to use the mutable keyword and make the entire public interface const.

    What's your opinion on doing that? Is it elegant or misleading? I feel that the user would prefer to use const methods, giving them a sense of read-only access to the managed resource.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do any of the member functions change the observable state of the object? If not, then having them be const and using mutable for the internal book keeping state is precisely what mutable is for.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Guest
    Guest
    I don't think they do.
    Code:
    // simplified
    std::string MyClass::operator[](size_t index) const noexcept
    {
        std::string output;
        // use this->std::ifstream member to fill "output"
        return output;
    }
    So in this case the std::ifstream is affected by seek/read operations, but no other class member is touched. Would that be an appropriate use of mutable/const?
    Last edited by Guest; 04-30-2016 at 04:21 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In this case, I'd say it's a good use of mutable/const.
    Question to ask oneself: If I repeatedly call this function on the same object, will I get back the same result every time? If so, then the observable state of the object hasn't changed (to me). Hence, you should consider using const there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Guest
    Guest
    Right. I also give access to- and overload std::ifstream::read through the public interface, and that function requires manual seeking and/or eofbit clearing to behave identical each call, so I won't mark it const then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Decltype", class member access level, and temporary objects
    By Grumpulus in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2014, 11:40 AM
  2. Getting "undeclared member" error with derived class
    By indigo0086 in forum C++ Programming
    Replies: 4
    Last Post: 10-07-2007, 06:41 AM
  3. Getting a:"member is not of class type" error
    By indigo0086 in forum C++ Programming
    Replies: 10
    Last Post: 11-20-2006, 10:19 AM
  4. "ambiguous access to class/struct/union member"
    By Mr_Jack in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2003, 12:15 PM
  5. Mutable members in const member functions still const
    By ripper079 in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2002, 08:56 AM