initialization of structs

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Sep 26 14:18:19 PDT 2011


You can hide the constructor from external code by using "private
this()". And you can disable the default constructor via @disable
this();
You may also wish to make your fields private as well.

You don't necessarily have to make createS() a free function, you can
make it a static function inside of S, ala:

struct S {
    @disable this();
    static S create(int _i) { return S(_i); }
private:
    int i;
    this(int i_) { i = i_; }
}

create() will not occupy any space in any of the S instances. If that
doesn't compile it's because I haven't had my coffee yet. :p


More information about the Digitalmars-d-learn mailing list