Re: class initialization
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.
Re: Class Initialization
The basic idea is that in D, any statically identifiable information (known at compile-time), can be used to assign class members as they are declared. Any time a new object is created, it will take those default values specified for its members. This is a small example demonstrating default initialization for both structs and classes. In fact, compared to the version of C++ I used to use (I can't speak for C++11), the syntax is far more consistent and less verbose than initializer lists. struct Dummy { int field1 = 10; int field2 = 11; } class MyClass { int a = 0; int[] b = [1, 2, 3]; Dummy c = Dummy(4, 5); int d = 6; this() { } this(int val) { d = val; } } void main() { MyClass first = new MyClass(); MyClass second = new MyClass(7); assert(first.a == 0); assert(first.b == [1, 2, 3]); assert(first.c.field1 == 4); assert(first.d == 6); assert(second.c.field1 == 4); assert(second.d == 7); } You are correct that in the case of the second constructor, two assignments effectively take place, d = 6, then d = 7. However, I do not think the compiler can know what you intend to do in the constructor, or even that you will not use the default value of d before reassigning it. In short, I think the optimization would tend to become more of a source of problems than a gain in performance in any meaningful way. - Vijay On Tue, 31 Jan 2012, Zachary Lund wrote: In C++, they provide a mechanism to initialize class variables to a passed value. class Test { int bob; public: Test(int jessica) : bob(jessica) { } }; The above basically says int this.bob = jessica; as opposed to this: class Test { int bob; public: Test(int jessica) { bob = jessica; } }; which basically says int this.bob = void; bob = jessica;. Now, I'm not a speed freak but this is a quick and should be a painless optimization. D allows defaults set by the class but cannot seem to find anything to allow me variable initialization values. Not that it's that big of a deal but am I missing something?
Re: Class Initialization
On 1/31/12, Jonathan M Davis jmdavisp...@gmx.com wrote: In D however, the _entire_ type is created and default initialized before the first constructor is called. I was reading the C++ Object Model book recently (well, I'm still reading) and was amazed at how many edge-cases there are in C++, and how lucky I am to be using D. Well, minus the Optlink that is. :)
Re: Class Initialization
On Tue, Jan 31, 2012 at 08:49:25PM +0100, Andrej Mitrovic wrote: On 1/31/12, Jonathan M Davis jmdavisp...@gmx.com wrote: In D however, the _entire_ type is created and default initialized before the first constructor is called. I was reading the C++ Object Model book recently (well, I'm still reading) and was amazed at how many edge-cases there are in C++, and how lucky I am to be using D. Well, minus the Optlink that is. :) OOP in C++ is so broken that I have given up on OO in C++ altogether. I just write C++ code as C with classes. I have come to hold the opinion that real OO cannot be done properly in C++ (not without lots and lots of pain, hacks, workarounds, and bandages, anyway). OO in Java is much saner. But I'm glad to say that OO in D is turning out to be much better so far. It's what C++ should have been but failed to be. T -- My program has no bugs! Only unintentional features...
Re: Class Initialization
I was reading the C++ Object Model book recently (well, I'm still reading) and was amazed at how many edge-cases there are in C++, and how lucky I am to be using D. Well, minus the Optlink that is. :) *whispers gee dee c*
Re: Class Initialization
On 01/31/2012 12:41 PM, Trass3r wrote: *whispers gee dee c* windows binaries please...
Re: Class Initialization
*whispers gee dee c* windows binaries please... Seek and you shall find. https://bitbucket.org/goshawk/gdc/downloads