www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - class initialization

reply Marc <jckj33 gmail.com> writes:
in C# you can initilizate the class members like this:

 var foo = new Foo { a = 1, b = 2 };
I found something similar to structs in D:
 myStruct S = {a:1, b:2};
But can't figure out if D does have that for classes.
Jan 15
parent thedeemon <dlang thedeemon.com> writes:
On Tuesday, 16 January 2018 at 03:23:20 UTC, Marc wrote:

 But can't figure out if D does have that for classes.
I believe there's no such thing for classes, you're supposed to use constructors. Class objects are in many aspects more abstract things than POD structs: instead of accessing their data directly there are constructors, virtual (by default) methods, and there is inheritance and interfaces which require the accesses to be rather abstract and indirect to work well.
Jan 16