const after initialization / pointers, references and values
Jakob Ovrum via Digitalmars-d
digitalmars-d at puremagic.com
Wed Aug 20 14:49:18 PDT 2014
On Wednesday, 20 August 2014 at 21:26:55 UTC, Philippe Sigaud via
Digitalmars-d wrote:
> If you want reference semantics but do not want to have
> pointers in
> your code, yes classes are your best choice.
Certainly the easiest, but I don't think it's always the best. If
light-weightedness is desired, make the struct contain the
reference, effectively making the struct a reference type:
---
private struct Data
{
int a;
string b;
double c;
}
/// Lorem ipsum ...
struct UserFacingType
{
private Data* data;
// use data.foo
}
---
Of course, this means UserFacingType.init is essentially `null`,
just like class references. By using std.typecons.RefCounted, the
user-facing type can also easily be made a reference-counted
reference.
Structs can do everything classes can with enough boilerplate.
Leverage templated types like RefCounted to remove the
boilerplate. Classes are just an in-built convenience feature to
handle the boilerplate of virtual functions.
More information about the Digitalmars-d
mailing list