forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.dd
1315 lines (1077 loc) · 35 KB
/
class.dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Ddoc
$(SPEC_S Classes,
$(P The object-oriented features of D all come from classes. The class
hierarchy
has as its root the class Object. Object defines a minimum level of functionality
that each derived class has, and a default implementation for that functionality.
)
$(P Classes are programmer defined types. Support for classes are what
make D an object oriented language, giving it encapsulation, inheritance,
and polymorphism. D classes support the single inheritance paradigm, extended
by adding support for interfaces. Class objects are instantiated by reference
only.
)
$(P A class can be exported, which means its name and all its
non-private
members are exposed externally to the DLL or EXE.
)
$(P A class declaration is defined:
)
$(GRAMMAR
$(GNAME ClassDeclaration):
$(D class) $(I Identifier) $(D ;)
$(D class) $(I Identifier) $(GLINK BaseClassList)$(OPT) $(GLINK2 struct, AggregateBody)
$(GLINK2 template, ClassTemplateDeclaration)
$(GNAME BaseClassList):
$(D :) $(GLINK SuperClass)
$(D :) $(GLINK SuperClass) $(D ,) $(GLINK Interfaces)
$(D :) $(GLINK Interfaces)
$(GNAME SuperClass):
$(GLINK2 declaration, BasicType)
$(GNAME Interfaces):
$(GLINK Interface)
$(GLINK Interface) $(D ,) $(I Interfaces)
$(GNAME Interface):
$(GLINK2 declaration, BasicType)
)
Classes consist of:
$(UL
$(LI a super class)
$(LI interfaces)
$(LI dynamic fields)
$(LI static fields)
$(LI types)
$(LI $(RELATIVE_LINK2 synchronized-classes, an optional synchronized attribute))
$(LI $(RELATIVE_LINK2 member-functions, member functions)
$(UL
$(LI static member functions)
$(LI $(DDSUBLINK function, virtual-functions, Virtual Functions))
$(LI $(RELATIVE_LINK2 constructors, Constructors))
$(LI $(RELATIVE_LINK2 destructors, Destructors))
$(LI $(RELATIVE_LINK2 static-constructor, Static Constructors))
$(LI $(RELATIVE_LINK2 static-destructor, Static Destructors))
$(LI $(GLINK SharedStaticConstructor)s)
$(LI $(GLINK SharedStaticDestructor)s)
$(LI $(RELATIVE_LINK2 invariants, Class Invariants))
$(LI $(DDSUBLINK unittest, unittest, Unit Tests))
$(LI $(RELATIVE_LINK2 allocators, Class Allocators))
$(LI $(RELATIVE_LINK2 deallocators, Class Deallocators))
$(LI $(RELATIVE_LINK2 alias-this, Alias This))
)
)
)
A class is defined:
------
class Foo
{
... members ...
}
------
$(P Note that there is no trailing $(D ;) after the closing $(D }) of the class
definition.
It is also not possible to declare a variable var like:)
------
class Foo { } var;
------
Instead:
------
class Foo { }
Foo var;
------
$(H3 Access Control)
$(P Access to class members is controlled using $(GLINK2 attribute, ProtectionAttribute)s.
The default protection attribute is $(D public).
Access control does not affect visibility.
)
$(H3 Fields)
$(P Class members are always accessed with the . operator.
)
$(P Members of a base class can be accessed by prepending the name of
the base class followed by a dot:)
---
class A { int a; }
class B : A { int a; }
void foo(B b)
{
b.a = 3; // accesses field B.a
b.A.a = 4; // accesses field A.a
}
---
$(P The D compiler is free to rearrange the order of fields in a class to
optimally pack them in an implementation-defined manner.
Consider the fields much like the local
variables in a function -
the compiler assigns some to registers and shuffles others around all to
get the optimal
stack frame layout. This frees the code designer to organize the fields
in a manner that
makes the code more readable rather than being forced to organize it
according to
machine optimization rules. Explicit control of field layout is provided
by struct/union
types, not classes.
)
$(H3 Field Properties)
$(P The $(D .offsetof) property gives the offset in bytes of the field
from the beginning of the class instantiation.
$(D .offsetof) can only be applied to
expressions which produce the type of
the field itself, not the class type:
)
------
class Foo
{
int x;
}
...
void test(Foo foo)
{
size_t o;
o = Foo.x.offsetof; // error, Foo.x needs a 'this' reference
o = foo.x.offsetof; // ok
}
------
$(H3 Class Properties)
$(P The $(D .tupleof) property returns an $(I ExpressionTuple)
of all the fields
in the class, excluding the hidden fields and the fields in the
base class.
)
---
class Foo { int x; long y; }
void test(Foo foo)
{
foo.tupleof[0] = 1; // set foo.x to 1
foo.tupleof[1] = 2; // set foo.y to 2
foreach (x; foo.tupleof)
write(x); // prints 12
}
---
$(P The properties $(D .__vptr) and $(D .__monitor) give access
to the class object's vtbl[] and monitor, respectively, but
should not be used in user code.
)
$(H3 Super Class)
All classes inherit from a super class. If one is not specified,
it inherits from Object. Object forms the root of the D class
inheritance hierarchy.
$(H3 $(LNAME2 member-functions, Member Functions))
$(P Non-static member functions have an extra hidden parameter
called $(I this) through which the class object's other members
can be accessed.
)
$(P Non-static member functions can have, in addition to the usual
$(GLINK2 function, FunctionAttribute)s, the attributes
$(D const), $(D immutable), $(D shared), or $(D inout).
These attributes apply to the hidden $(I this) parameter.
)
---
class C
{
int a;
const void foo()
{
a = 3; // error, 'this' is const
}
void foo() immutable
{
a = 3; // error, 'this' is immutable
}
}
---
$(H3 $(LNAME2 synchronized-classes, Synchronized Classes))
$(P All member functions of synchronized classes are synchronized.
A static member function is synchronized on the $(I classinfo)
object for the class, which means that one monitor is used
for all static member functions for that synchronized class.
For non-static functions of a synchronized class, the monitor
used is part of the class object. For example:
)
---
synchronized class Foo
{
void bar() { ...statements... }
}
---
$(P is equivalent to (as far as the monitors go):
)
---
synchronized class Foo
{
void bar()
{
synchronized (this) { ...statements... }
}
}
---
$(P Member functions of non-synchronized classes cannot be individually marked as synchronized.
The synchronized attribute must be applied to the class declaration itself:
)
---
class Foo
{
synchronized void foo() { } // disallowed!
}
synchronized class Bar
{
void bar() { } // bar is synchronized
}
---
$(P Member fields of a synchronized class cannot be public:
)
---
synchronized class Foo
{
int foo; // disallowed: public field
}
synchronized class Bar
{
private int bar; // ok
}
---
$(P The $(D synchronized) attribute can only be applied to classes,
structs cannot be marked to be synchronized.)
$(H3 $(LNAME2 constructors, Constructors))
$(GRAMMAR
$(GNAME Constructor):
$(D this) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(D ;)
$(D this) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
$(GLINK2 template, ConstructorTemplate)
)
$(P Members are always initialized to the
$(LNAME2 class-default-initializer, default initializer)
for their type, which is usually 0 for integer types and
NAN for floating point types.
This eliminates an entire
class of obscure problems that come from
neglecting to initialize a member in one of the constructors.
In the class definition,
there can be a static initializer to be
used instead of the default:
)
------
class Abc
{
int a; // default initializer for a is 0
long b = 7; // default initializer for b is 7
float f; // default initializer for f is NAN
}
------
This static initialization is done before any constructors are
called.
$(P Constructors are defined with a function name of $(D this)
and having no return value:)
------
class Foo
{
$(CODE_HIGHLIGHT this)(int x) // declare constructor for Foo
{ ...
}
$(CODE_HIGHLIGHT this)()
{ ...
}
}
------
Base class construction is done by calling the base class
constructor by the name $(D super):
------
class A { this(int y) { } }
class B : A
{
int j;
this()
{
...
$(CODE_HIGHLIGHT super)(3); // call base constructor A.this(3)
...
}
}
------
$(P Constructors can also call other constructors for the same class
in order to share common initializations
$(LNAME2 delegating-constructors, (this is called delegating constructors)):
)
------
class C
{
int j;
this()
{
...
}
this(int i)
{
$(CODE_HIGHLIGHT this)();
j = i;
}
}
------
If no call to constructors via $(D this) or $(D super) appear
in a constructor, and the base class has a constructor, a call
to $(D super)() is inserted at the beginning of the constructor.
$(P If there is no constructor for a class, but there is a constructor
for the base class, a default constructor of the form:)
------
this() { }
------
$(P is implicitly generated.)
$(P Class object construction is very flexible, but some restrictions
apply:)
$(OL
$(LI It is illegal for constructors to mutually call each other,
although the compiler is not required to detect it. It will result
in undefined behavior.
------
this() { this(1); }
this(int i) { this(); } // illegal, cyclic constructor calls
------
)
$(LI If any constructor call appears inside a constructor, any
path through the constructor must make exactly one constructor
call:
------
this() { a || super(); } // illegal
this() { (a) ? this(1) : super(); } // ok
this()
{
for (...)
{
super(); // illegal, inside loop
}
}
------
)
$(LI It is illegal to refer to $(D this) implicitly or explicitly
prior to making a constructor call.)
$(LI Constructor calls cannot appear after labels (in order to make
it easy to check for the previous conditions in the presence of goto's).)
)
$(P Instances of class objects are created with $(I NewExpression)s:)
------
A a = new A(3);
------
$(P The following steps happen:)
$(OL
$(LI Storage is allocated for the object.
If this fails, rather than return $(D null), an
$(D OutOfMemoryError) is thrown.
Thus, tedious checks for null references are unnecessary.
)
$(LI The raw data is statically initialized using the values provided
in the class definition.
The pointer to the vtbl[] (the array of pointers to virtual functions)
is assigned.
This ensures that constructors are
passed fully formed objects for which virtual functions can be called.
This operation is equivalent to doing a memory copy of a static
version of the object onto the newly allocated one,
although more advanced compilers
may be able to optimize much of this away.
)
$(LI If there is a constructor defined for the class,
the constructor matching the
argument list is called.
)
$(LI If class invariant checking is turned on, the class invariant
is called at the end of the constructor.
)
)
$(P Constructors can have one of these member function attributes:
$(D const), $(D immutable), and $(D shared). Construction of qualified
objects will then be restricted to the implemented qualified constructors.
)
------
class C
{
this(); // non-shared mutable constructor
}
// create mutable object
C m = new C();
// create const object using by mutable constructor
const C c2 = new const C();
// a mutable constructor cannot create an immutable object
// immutable C i = new immutable C();
// a mutable constructor cannot create a shared object
// shared C s = new shared C();
------
$(P Constructors can be overloaded with different attributes.
)
------
class C
{
this(); // non-shared mutable constructor
this() shared; // shared mutable constructor
this() immutable; // immutable constructor
}
C m = new C();
shared s = new shared C();
immutable i = new immutable C();
------
$(P If the constructor can create unique object (e.g. if it is pure),
the object can be implicitly convertible to any qualifiers.
)
------
class C
{
this() pure;
// Based on the definition, this creates a mutable object. But the
// created object cannot contain any mutable global data.
// Then compiler can guarantee that the created object is unique.
this(int[] arr) immutable pure;
// Based on the definition, this creates an immutable object. But
// the argument int[] never appears in the created object so it
// isn't implicitly convertible to immutable. Also, it cannot store
// any immutable global data.
// Therefore the compiler can guarantee that the created object is
// unique.
}
immutable i = new immutable C(); // this() pure is called
shared s = new shared C(); // this() pure is called
C m = new C([1,2,3]); // this(int[]) immutable pure is called
------
$(H3 $(LNAME2 field-init, Field initialization inside constructor))
$(P Inside constructor, the first instance field assignment is specially
handled for its initialization.
)
------
class C
{
int num;
this()
{
num = 1; // initialize
num = 2; // assignment
}
}
------
$(P If the field type has opAssign method, it won't be used for initialization.)
------
struct A
{
this(int n) {}
void opAssign(A rhs) {}
}
class C
{
A val;
this()
{
val = A(1); // A(1) is moved in this.val for initializing
val = A(2); // rewritten to val.opAssign(A(2))
}
}
------
$(P If the field type is not modifiable, multiple initialization will be rejected.)
------
class C
{
immutable int num;
this()
{
num = 1; // OK
num = 2; // Error: multiple field initialization
}
}
------
$(P If the assignment expression for the field initialization may be invoked
multiple times, it would als be rejected.
)
------
class C
{
immutable int num;
immutable string str;
this()
{
foreach (i; 0..2)
{
num = 1; // Error: field initialization not allowed in loops
}
size_t i = 0;
Label:
str = "hello"; // Error: field initialization not allowed after labels
if (i++ < 2)
goto Label;
}
}
------
$(H3 $(LNAME2 destructors, Destructors))
$(GRAMMAR
$(GNAME Destructor):
$(D ~ this ( )) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(D ;)
$(D ~ this ( )) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
)
The garbage collector calls the destructor function when the object
is deleted. The syntax
is:
------
class Foo
{
~this() // destructor for Foo
{
}
}
------
$(P There can be only one destructor per class, the destructor
does not have any parameters,
and has no attributes. It is always virtual.
)
$(P The destructor is expected to release any resources held by the
object.
)
$(P The program can explicitly inform the garbage collector that an
object is no longer referred to (with the delete expression), and
then the garbage collector calls the destructor
immediately, and adds the object's memory to the free storage.
The destructor is guaranteed to never be called twice.
)
$(P The destructor for the super class automatically gets called when
the destructor ends. There is no way to call the super destructor
explicitly.
)
$(P The garbage collector is not guaranteed to run the destructor
for all unreferenced objects. Furthermore, the order in which the
garbage collector calls destructors for unreference objects
is not specified.
This means that
when the garbage collector calls a destructor for an object of a class
that has
members that are references to garbage collected objects, those
references may no longer be valid. This means that destructors
cannot reference sub objects.
This rule does not apply to auto objects or objects deleted
with the $(I DeleteExpression), as the destructor is not being run
by the garbage collector, meaning all references are valid.
)
$(P Objects referenced from the data segment never get collected
by the gc.
)
$(H3 $(LNAME2 static-constructor, Static Constructors))
$(GRAMMAR
$(GNAME StaticConstructor):
$(D static this ( )) $(D ;)
$(D static this ( )) $(GLINK2 function, FunctionBody)
)
$(P
A static constructor is a function that performs
initializations of thread local data before the $(D main()) function gets control for
the main thread, and upon thread startup.
Static constructors are used to initialize
static class members
with values that cannot be computed at compile time.
)
$(P Static constructors in other languages are built implicitly by using
member
initializers that can't be computed at compile time. The trouble with
this stems from not
having good control over exactly when the code is executed, for example:
)
------
class Foo
{
static int a = b + 1;
static int b = a * 2;
}
------
What values do a and b end up with, what order are the initializations
executed in, what
are the values of a and b before the initializations are run, is this a
compile error, or is this
a runtime error? Additional confusion comes from it not being obvious if
an initializer is
static or dynamic.
$(P D makes this simple. All member initializations must be determinable by
the compiler at
compile time, hence there is no order-of-evaluation dependency for
member
initializations, and it is not possible to read a value that has not
been initialized. Dynamic
initialization is performed by a static constructor, defined with
a special syntax $(D static this()).)
------
class Foo
{
static int a; // default initialized to 0
static int b = 1;
static int c = b + a; // error, not a constant initializer
$(CODE_HIGHLIGHT static this)() // static constructor
{
a = b + 1; // a is set to 2
b = a * 2; // b is set to 4
}
}
------
$(P
If $(D main()) or the thread returns normally,
(does not throw an exception), the static destructor is added
to the list of functions to be
called on thread termination.
Static constructors have empty parameter lists.
)
$(P
Static constructors within a module are executed in the lexical
order in which they appear.
All the static constructors for modules that are directly or
indirectly imported
are executed before the static constructors for the importer.
)
$(P
The $(D static) in the static constructor declaration is not
an attribute, it must appear immediately before the $(D this):
)
------
class Foo
{
static this() { ... } // a static constructor
static private this() { ... } // not a static constructor
static
{
this() { ... } // not a static constructor
}
static:
this() { ... } // not a static constructor
}
------
$(H3 $(LNAME2 static-destructor, Static Destructors))
$(GRAMMAR
$(GNAME StaticDestructor):
$(D static ~ this ( )) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(D ;)
$(D static ~ this ( )) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
)
A static destructor is defined as a special static function with the
syntax $(D static ~this()).
------
class Foo
{
static ~this() // static destructor
{
}
}
------
$(P
A static destructor gets called on thread termination,
but only if the static constructor
completed successfully.
Static destructors have empty parameter lists.
Static destructors get called in the reverse order that the static
constructors were called in.
)
$(P
The $(D static) in the static destructor declaration is not
an attribute, it must appear immediately before the $(D ~this):
)
------
class Foo
{
static ~this() { ... } // a static destructor
static private ~this() { ... } // not a static destructor
static
{
~this() { ... } // not a static destructor
}
static:
~this() { ... } // not a static destructor
}
------
$(H3 Shared Static Constructors)
$(GRAMMAR
$(GNAME SharedStaticConstructor):
$(D shared static this ( )) $(D ;)
$(D shared static this ( )) $(GLINK2 function, FunctionBody)
)
$(P Shared static constructors are executed before any $(GLINK StaticConstructor)s,
and are intended for initializing any shared global data.
)
$(H3 Shared Static Destructors)
$(GRAMMAR
$(GNAME SharedStaticDestructor):
$(D shared static ~ this ( )) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(D ;)
$(D shared static ~ this ( )) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
)
$(P Shared static destructors are executed at program termination
in the reverse order that
$(GLINK SharedStaticConstructor)s were executed.
)
$(H3 $(LNAME2 invariants, Class Invariants))
$(GRAMMAR
$(GNAME Invariant):
$(D invariant ( )) $(GLINK2 statement, BlockStatement)
$(D invariant) $(GLINK2 statement, BlockStatement)
)
Class invariants are used to specify characteristics of a class that always
must be true (except while executing a member function).
They are described in $(GLINK2 contracts, Invariants).
$(H3 $(LNAME2 allocators, Class Allocators))
$(B Note): Class allocators are deprecated in D2.
$(GRAMMAR
$(GNAME Allocator):
$(D new) $(GLINK2 function, Parameters) $(D ;)
$(D new) $(GLINK2 function, Parameters) $(GLINK2 function, FunctionBody)
)
A class member function of the form:
------
new(uint size)
{
...
}
------
is called a class allocator.
The class allocator can have any number of parameters, provided
the first one is of type uint.
Any number can be defined for a class, the correct one is
determined by the usual function overloading rules.
When a new expression:
------
new Foo;
------
is executed, and Foo is a class that has
an allocator, the allocator is called with the first argument
set to the size in bytes of the memory to be allocated for the
instance.
The allocator must allocate the memory and return it as a
$(D void*).
If the allocator fails, it must not return a $(D null), but
must throw an exception.
If there is more than one parameter to the allocator, the
additional arguments are specified within parentheses after
the $(D new) in the $(I NewExpression):
------
class Foo
{
this(char[] a) { ... }
new(uint size, int x, int y)
{
...
}
}
...
new(1,2) Foo(a); // calls new(Foo.sizeof,1,2)
------
$(P Derived classes inherit any allocator from their base class,
if one is not specified.
)
$(P The class allocator is not called if the instance is created
on the stack.
)
$(P See also
$(DDLINK1 memory, newdelete, Explicit Class Instance Allocation).
)
$(H3 $(LNAME2 deallocators, Class Deallocators))
$(B Note): Class deallocators and the delete operator are deprecated in D2.
Use the $(D destroy) function to finalize an object by calling its destructor.
The memory of the object is $(B not) immediately deallocated, instead the GC
will collect the memory of the object at an undetermined point after finalization:
------
class Foo { int x; this() { x = 1; } }
Foo foo = new Foo;
destroy(foo);
assert(foo.x == int.init); // object is still accessible
------
$(GRAMMAR
$(GNAME Deallocator):
$(D delete) $(GLINK2 function, Parameters) $(D ;)
$(D delete) $(GLINK2 function, Parameters) $(GLINK2 function, FunctionBody)
)
A class member function of the form:
------
delete(void *p)
{
...
}
------
is called a class deallocator.
The deallocator must have exactly one parameter of type $(D void*).
Only one can be specified for a class.
When a delete expression:
------
delete f;
------
$(P is executed, and f is a reference to a class instance that has
a deallocator, the deallocator is called with a pointer to the
class instance after the destructor (if any) for the class is
called. It is the responsibility of the deallocator to free
the memory.
)
$(P Derived classes inherit any deallocator from their base class,
if one is not specified.
)
$(P The class allocator is not called if the instance is created
on the stack.
)
$(P See also
$(DDSUBLINK memory, newdelete, Explicit Class Instance Allocation).
)
$(H3 $(LEGACY_LNAME2 AliasThis, alias-this, Alias This))
$(GRAMMAR
$(GNAME AliasThis):
$(D alias) $(I Identifier) $(D this ;)
)
$(P An $(I AliasThis) declaration names a member to subtype.
The $(I Identifier) names that member.
)
$(P A class or struct can be implicitly converted to the $(I AliasThis)
member.
)
---
struct S
{
int x;
alias x this;
}
int foo(int i) { return i * 2; }
void test()
{