@@ -954,26 +954,45 @@ $(H2 $(LNAME2 virtual-functions, Virtual Functions))
954
954
955
955
$(P Virtual functions are class member functions that are called indirectly through a
956
956
function pointer table, called a `vtbl[]`, rather than directly.
957
+ Member functions that are virtual can be overridden in a derived class:
957
958
)
958
959
959
- $(P Member functions that are virtual can be overridden, unless they are `final`.
960
- )
960
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
961
+ ------
962
+ class A
963
+ {
964
+ void foo(int x) {}
965
+ }
961
966
962
- $(P Struct and union member functions are not virtual.
963
- )
967
+ class B : A
968
+ {
969
+ override void foo(int x) {}
970
+ //override void foo() {} // error, no foo() in A
971
+ }
964
972
965
- $(P Static member functions are not virtual.
966
- )
973
+ void test()
974
+ {
975
+ A a = new B();
976
+ a.foo(1); // calls B.foo(int)
977
+ }
978
+ ------
979
+ )
967
980
968
- $(P Member functions which are $(D private) or $(D package) are not virtual.
969
- )
981
+ $(P The `override` attribute is required when overriding a function.
982
+ This is useful for catching errors when a base class's member function
983
+ has its parameters changed, and all derived classes need to have
984
+ their overriding functions updated.)
970
985
971
- $(P Member template functions are not virtual.
986
+ $(P The following are not virtual:)
987
+ $(UL
988
+ $(LI Struct and union member functions)
989
+ $(LI `final` member functions)
990
+ $(LI $(DDSUBLINK attribute, static, `static`) member functions)
991
+ $(LI Member functions which are $(D private) or $(D package))
992
+ $(LI Member template functions)
972
993
)
973
994
974
- $(P Member functions with `Objective-C` linkage are virtual even if marked
975
- with `final` or `static`.
976
- )
995
+ $(P $(B Example:))
977
996
978
997
------
979
998
class A
@@ -992,22 +1011,23 @@ class B : A
992
1011
int abc() { ... } // ok, A.abc is not virtual, B.abc is virtual
993
1012
}
994
1013
995
- void test(A a )
1014
+ void test()
996
1015
{
1016
+ A a = new B;
997
1017
a.def(); // calls B.def
998
1018
a.foo(); // calls A.foo
999
1019
a.bar(); // calls A.bar
1000
1020
a.abc(); // calls A.abc
1001
1021
}
1002
-
1003
- void func()
1004
- {
1005
- B b = new B();
1006
- test(b);
1007
- }
1008
1022
------
1009
1023
1010
- $(P The overriding function may be covariant with the overridden function.
1024
+ $(P Member functions with `Objective-C` linkage are virtual even if marked
1025
+ with `final` or `static`, and can be overridden.
1026
+ )
1027
+
1028
+ $(H3 $(LNAME2 covariance, Covariance))
1029
+
1030
+ $(P An overriding function may be covariant with the overridden function.
1011
1031
A covariant function has a type that is implicitly convertible to the
1012
1032
type of the overridden function.
1013
1033
)
@@ -1030,21 +1050,12 @@ class Bar : Foo
1030
1050
------
1031
1051
)
1032
1052
1033
- $(P
1034
- Non-static (i.e., virtual functions and `final` member
1035
- functions) all have a hidden parameter called the
1036
- `this` reference, which refers to the class object for which
1037
- the function is called.
1038
- )
1053
+ $(H3 $(LNAME2 base-methods, Calling Base Class Methods))
1039
1054
1040
- $(P
1041
- Functions with `Objective-C` linkage have an additional hidden,
1042
- unnamed, parameter which is the selector it was called with.
1043
- )
1044
-
1045
- $(P To directly call a base member function (i.e., avoid dynamic
1046
- binding on member function call), insert the base class name before
1047
- the member function name. For example:
1055
+ $(P To directly call a member function of a base class `Base`,
1056
+ write `Base.` before the function name.
1057
+ This avoids dynamic dispatch through a function pointer. For
1058
+ example:
1048
1059
)
1049
1060
1050
1061
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
@@ -1086,33 +1097,7 @@ void main()
1086
1097
known, such as if it is `final`, it can use a direct call instead.
1087
1098
)
1088
1099
1089
- $(H3 $(LNAME2 function-inheritance, Function Inheritance and Overriding))
1090
-
1091
- $(P A function in a derived class with the same name and covariant
1092
- with a function in a base class overrides that function:)
1093
-
1094
- ------
1095
- class A
1096
- {
1097
- int foo(int x) { ... }
1098
- }
1099
-
1100
- class B : A
1101
- {
1102
- override int foo(int x) { ... }
1103
- }
1104
-
1105
- void test()
1106
- {
1107
- B b = new B();
1108
- bar(b);
1109
- }
1110
-
1111
- void bar(A a)
1112
- {
1113
- a.foo(1); // calls B.foo(int)
1114
- }
1115
- ------
1100
+ $(H3 $(LNAME2 function-inheritance, Overload Sets and Overriding))
1116
1101
1117
1102
$(P When doing overload resolution, the functions in the base
1118
1103
class are not considered, as they are not in the same
@@ -1134,9 +1119,9 @@ class B : A
1134
1119
void test()
1135
1120
{
1136
1121
B b = new B();
1137
- b.foo(1); // calls B.foo(long), since A.foo(int) not considered
1138
- A a = b;
1122
+ b.foo(1); // calls B.foo(long), since A.foo(int) is not considered
1139
1123
1124
+ A a = b;
1140
1125
a.foo(1); // issues runtime error (instead of calling A.foo(int))
1141
1126
}
1142
1127
------
@@ -1160,12 +1145,7 @@ class B : A
1160
1145
1161
1146
void test()
1162
1147
{
1163
- B b = new B();
1164
- bar(b);
1165
- }
1166
-
1167
- void bar(A a)
1168
- {
1148
+ A a = new B();
1169
1149
a.foo(1); // calls A.foo(int)
1170
1150
B b = new B();
1171
1151
b.foo(1); // calls A.foo(int)
@@ -1180,6 +1160,8 @@ void bar(A a)
1180
1160
implicit conversions to the base class, those other functions do
1181
1161
get called:
1182
1162
)
1163
+
1164
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
1183
1165
---
1184
1166
class A
1185
1167
{
@@ -1188,22 +1170,19 @@ class A
1188
1170
}
1189
1171
class B : A
1190
1172
{
1191
- void set(long i) { }
1173
+ override void set(long i) { }
1192
1174
}
1193
1175
1194
- void foo(A a )
1176
+ void test( )
1195
1177
{
1196
- int i ;
1178
+ A a = new B ;
1197
1179
a.set(3); // error, use of A.set(int) is hidden by B
1198
1180
// use 'alias set = A.set;' to introduce base class overload set
1199
- assert(i == 1);
1200
- }
1201
-
1202
- void main()
1203
- {
1204
- foo(new B);
1205
1181
}
1206
1182
---
1183
+ )
1184
+
1185
+ $(H3 $(LNAME2 override-defaults, Default Values))
1207
1186
1208
1187
$(P A function parameter's default value is not inherited:)
1209
1188
@@ -1236,6 +1215,8 @@ void test()
1236
1215
}
1237
1216
------
1238
1217
1218
+ $(H3 $(LNAME2 inheriting-attributes, Inherited Attributes))
1219
+
1239
1220
$(P An overriding function inherits any unspecified $(GLINK FunctionAttributes)
1240
1221
from the attributes of the overridden function.)
1241
1222
@@ -1258,6 +1239,8 @@ void main()
1258
1239
------
1259
1240
)
1260
1241
1242
+ $(H3 $(LNAME2 override-restrictions, Restrictions))
1243
+
1261
1244
$(P The attributes
1262
1245
$(LINK2 attribute.html#disable, $(D @disable)) and
1263
1246
$(LINK2 attribute.html#deprecated, $(D deprecated))
@@ -1281,7 +1264,6 @@ void main()
1281
1264
}
1282
1265
---
1283
1266
1284
- $(P Static functions with `Objective-C` linkage are overridable.)
1285
1267
1286
1268
$(H2 $(LNAME2 inline-functions, Inline Functions))
1287
1269
@@ -2414,6 +2396,19 @@ $(H4 $(LNAME2 lazy_variadic_functions, Lazy Variadic Functions))
2414
2396
parameter. This will prevent a closure being generated for the delegate,
2415
2397
as `scope` means the delegate will not escape the function.)
2416
2398
2399
+ $(H3 $(LNAME2 this-reference, `this` Reference))
2400
+
2401
+ $(P
2402
+ Non-static member functions all have a hidden parameter called the
2403
+ `this` reference, which refers to the object for which
2404
+ the function is called.
2405
+ )
2406
+
2407
+ $(P
2408
+ Functions with `Objective-C` linkage have an additional hidden,
2409
+ unnamed, parameter which is the selector it was called with.
2410
+ )
2411
+
2417
2412
2418
2413
$(H2 $(LEGACY_LNAME2 Local Variables, local-variables, Local Variables))
2419
2414
0 commit comments