开发者

When have you used C++ 'mutable' keyword? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 2 years ago.

开发者_运维问答 Improve this question

When have you used C++ mutable keyword? and why? I don't think I have ever had to use that keyword. I understand it is used for things such as caching (or perhaps memorization) but in what class and condition have you ever needed to use it in?


Occasionally I use it to mark a mutex or other thread synchronisation primitive as being mutable so that accessors/query methods, which are typically marked const can still lock the mutex.

It's also sometimes useful when you need to instrument your code for debugging or testing purposes, because instrumentation often needs to modify auxiliary data from inside query methods.


I've used mutable in case of object caching results calculated from internal members:

class Transformation
{
    private:
        vec3 translation;
        vec3 scale;
        vec4 rotation;
        mutable mat4 transformation;
        mutable bool changed;
    public:
        Node()
        {
            [...]
            changed = false;
        }
        void set_translation(vec3 _translation)
        {
            translation = _translation;
            changed = true;
        }
        void set_scale(...) ...


        mat4 get_transformation() const
        {
            if(changed)
            {
                 // transformation and changed need to be mutable here
                 transformation = f(translation, scale, rotation); // This take a long time...
                 changed = false;
            }
            return transformation;
        }
};

void apply_tranformation(const Transformation* transfo)
{
    apply(transfo->get_transformation());
}


Google code search reveals a number of uses. For example, in an implementation of XTR cryptography, mutable members are used so that methods can return a reference to a result (preventing copies from being made).

For another example, Webkit uses it to lazily initialize member data (m_lineHeight).


In mock objects to capture the value of arguments of const functions in a member variable.

class Source
{
public:
    virtual ~Source() {}
    virtual std::string read(int count) const=0;
};

class SourceMock : public Source
{
public:
    mutable std::vector<int> arguments;
    std::string read(int count) const {
        arguments.push_back(count);
        return "...";
    }
};

//TEST....
SourceMock mock;
//...
VERIFY(mock.arguments.size()==2);
VERIFY(mock.arguments[0]==3);
//...


I use mutable for class members that are initialized on demand, especially from a database or source external to the program. This allows the "getter" functions to create the object on demand otherwise it is a constant method.


I use it when locking a mutex for thread-safety. The mutex is marked as mutable so the methods that lock it can remain const.


http://msdn.microsoft.com/en-us/library/4h2h0ktk%28v=vs.80%29.aspx would be the best example. Hey, I have learned something today!


mutable keyword allows you to modify a variable inside a const context

e.g:

class Person {
private:
      mutable int age;

public:
    Person(int age) : age(age)  {

    }
    void setAge(int age) const {
        Person::age = age;
    }

    int getAge() const {
        return Person::age;
    }
};

int main() {

    Person person(23);
    std::cout << "Person age " << person.getAge() << std::endl;
    person.setAge(24);
    std::cout << "Person modified age " << person.getAge() << std::endl;
    return 0;
}


My template class implements reference-counter pattern. When it is passed to functions as argument with const modifier it is possible that reference can be increased anyway. So instead of const_cast mutable can be used.


It can be used in many scenarios e.g

Loggers, timing, access counters etc.

These can be called in const-qualified accessors without changing the state of the data.


It can also be used if inside a getter method (that is usually const), you need to update the stored returned value. As an example, suppose that you have an specific linked-list implementation. For performance issues, you keep the last calculated length of the list, but while returning the length, if the list has been modified, you compute it again, otherwise, you return the last cached value.

class MyLinkedList
{
public:
  long getLength() const
  {
    if (lengthIsModified())
    {
      mLength = ...; // do the computation here
    }
    return mLength;
  }
private:
  mutable long mLength;
};

Warning: It is not that easy to always keep mLength up-to-date because of some specific operations (like merging) in the list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜