What is a mutable method?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 22 08:46:57 PDT 2015


On Friday, May 22, 2015 12:12:45 tcak via Digitalmars-d-learn wrote:
> I know there is mutable variables, but what is a mutable method?
>
> Error message says "mutable method
> project.mariadb.connector.ver2p1.resultset.ResultSetColumn.info
> is not callable using a const object".

It's a method / member function which is not const or immutable. e.g.

struct MyStruct
{
    void foo() {}  // This method is mutable
    void bar() const {}
    void baz() immutable {}
}

What it's really indicating is the constness of the object's this point is
inside the function. So, you can only call a mutable method on a mutable
object, because otherwise, you'd be converting a const or immutable
reference or pointer to mutable, which would violate the type system.
mutable and immutable can be converted to const, but mutable and const
can't be converted to immutable, and const and immutable can't be converted
to mutable.

So, in your case, it sounds like you're trying to call a mutable method on a
const object, which won't work. The method needs to be const, since the
object is const.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list