digitalmars.D.learn - C++-style mutable members
- =?UTF-8?B?Tm9yZGzDtnc=?= (1/1) Jan 17 2017 Is there a way to mimic C++-style `mutable` members in D?
- ketmar (4/5) Jan 17 2017 sure: don't use `const`. otherwise — no, there is simply no way
- =?UTF-8?B?Tm9yZGzDtnc=?= (3/7) Jan 17 2017 I'm aware of the difference. I'm just making sure there are no
- ketmar (3/10) Jan 17 2017 besides brutally raping it with `cast` is accessor methods — no.
- Namespace (28/29) Jan 17 2017 You could store a ptr to the member outside:
Is there a way to mimic C++-style `mutable` members in D?
Jan 17 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?sure: don't use `const`. otherwise — no, there is simply no way to «mimic» 'em due to completely different meaning of `const` in D and C++.
Jan 17 2017
On Tuesday, 17 January 2017 at 20:47:35 UTC, ketmar wrote:I'm aware of the difference. I'm just making sure there are no ways out of the type system ;)Is there a way to mimic C++-style `mutable` members in D?sure: don't use `const`. otherwise — no, there is simply no way to «mimic» 'em due to completely different meaning of `const` in D and C++.
Jan 17 2017
On Tuesday, 17 January 2017 at 20:50:34 UTC, Nordlöw wrote:On Tuesday, 17 January 2017 at 20:47:35 UTC, ketmar wrote:besides brutally raping it with `cast` is accessor methods — no. ;-)I'm aware of the difference. I'm just making sure there are no ways out of the type system ;)Is there a way to mimic C++-style `mutable` members in D?sure: don't use `const`. otherwise — no, there is simply no way to «mimic» 'em due to completely different meaning of `const` in D and C++.
Jan 17 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); } ----
Jan 17 2017