@@ -382,14 +382,7 @@ $(H3 $(LNAME2 synchronized-classes, Synchronized Classes))
382
382
$(NOTE struct types cannot be marked `synchronized`.)
383
383
384
384
385
- $(H2 $(LNAME2 constructors, Constructors))
386
-
387
- $(GRAMMAR
388
- $(GNAME Constructor):
389
- $(D this) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
390
- $(D this) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, MissingFunctionBody)
391
- $(GLINK2 template, ConstructorTemplate)
392
- )
385
+ $(H2 $(LNAME2 class-instantiation, Class Instantiation))
393
386
394
387
$(P Fields are by default initialized to the
395
388
$(DDSUBLINK spec/property, init, default initializer)
@@ -398,34 +391,74 @@ $(GNAME Constructor):
398
391
If the field declaration has an optional $(GLINK2 declaration, Initializer)
399
392
that will be used instead of the default.
400
393
)
394
+
395
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
401
396
------
402
397
class Abc
403
398
{
404
399
int a; // default initializer for a is 0
405
400
long b = 7; // default initializer for b is 7
406
401
float f; // default initializer for f is NAN
407
402
}
403
+
404
+ void main()
405
+ {
406
+ Abc obj = new Abc;
407
+ assert(obj.b == 7);
408
+ }
408
409
------
410
+ )
409
411
410
412
$(P The $(I Initializer) is evaluated at compile time.)
411
413
412
- $(P This initialization is done before any constructors are
413
- called.)
414
+ $(P This initialization is done before any
415
+ $(RELATIVE_LINK2 constructors, constructors) are called.)
416
+
417
+ $(P Instances of class objects are created with a $(GLINK2 expression, NewExpression).)
418
+
419
+ * A class `C` without a constructor (or with a nullary constructor) is instantiated
420
+ using `new C`, without passing any arguments.
421
+ * Otherwise, a class can be instantiated with an argument list, e.g.
422
+ `new C(arguments)`. The arguments are passed to a constructor with a matching
423
+ parameter list (and resolved like $(DDSUBLINK spec/function, function-overloading,
424
+ overloaded methods)).
425
+
426
+ $(P By default, a class instance is allocated on the garbage-collected heap.
427
+ A $(DDSUBLINK spec/attribute, scope-class-var, `scope` class instance)
428
+ is allocated on the stack.)
429
+
430
+
431
+ $(H3 $(LNAME2 constructors, Constructors))
432
+
433
+ $(GRAMMAR
434
+ $(GNAME Constructor):
435
+ $(D this) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
436
+ $(D this) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, MissingFunctionBody)
437
+ $(GLINK2 template, ConstructorTemplate)
438
+ )
414
439
415
440
$(P Constructors are defined with a function name of $(D this)
416
441
and have no return value:)
417
442
443
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
418
444
------
419
- class Foo
445
+ class A
420
446
{
421
- $(CODE_HIGHLIGHT this)( int x) // declare constructor for Foo
422
- { ...
423
- }
424
- $(CODE_HIGHLIGHT this)()
425
- { ...
447
+ int i;
448
+
449
+ this(int i) // constructor taking an int argument
450
+ {
451
+ this.i = i; // initialize field `i` from parameter `i`
426
452
}
427
453
}
454
+
455
+ void main()
456
+ {
457
+ A a = new A(3);
458
+ assert(a.i == 3);
459
+ }
428
460
------
461
+ )
429
462
430
463
$(H3 $(LNAME2 base-construction, Base Class Construction))
431
464
@@ -528,16 +561,7 @@ $(H3 $(LNAME2 implicit-base-construction, Implicit Base Class Construction))
528
561
base class has a constructor that requires arguments and no
529
562
nullary constructor, a matching call to `super` is required.)
530
563
531
- $(H3 $(LNAME2 class-instantiation, Class Instantiation))
532
-
533
- $(P Instances of class objects are created with a $(GLINK2 expression, NewExpression):)
534
-
535
- ------
536
- A a = new A(3);
537
- ------
538
-
539
- $(P A $(DDSUBLINK spec/attribute, scope-class-var, `scope` class instance)
540
- is allocated on the stack.)
564
+ $(H3 $(LNAME2 instantiation-process, Instantiation Process))
541
565
542
566
$(P The following steps happen:)
543
567
@@ -568,8 +592,8 @@ $(H3 $(LNAME2 class-instantiation, Class Instantiation))
568
592
569
593
$(LI The body of the constructor is executed.)
570
594
571
- $(LI If class invariant checking is turned on, the class invariant
572
- is called at the end of the constructor.
595
+ $(LI If class invariant checking is turned on, the
596
+ $(RELATIVE_LINK2 invariants, class invariant) is called at the end of the constructor.
573
597
)
574
598
)
575
599
0 commit comments