C++-style mutable members

Namespace via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 17 13:40:46 PST 2017


On Tuesday, 17 January 2017 at 20:21:34 UTC, Nordlöw wrote:
> Is there a way to mimic C++-style `mutable` members in D?

You could store a ptr to the member outside:

----
import std.stdio;

private int* _pid;

struct A
{
	int id;
	
	this(int id)
	{
		this.id = id;
		_pid = &this.id;
	}
	
	void foo() const
	{
		(*_pid)++;
	}
}

void main()
{
	A a = A(42);
	a.foo();
	
	writeln(a.id);
}
----


More information about the Digitalmars-d-learn mailing list