2 Lightweight C++, version 0.6
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 This text documents the new features that appeared in 0.6
6 There are also the abstract template classes and the
7 virtual inheritance declarations which are presented in
11 0. Accessing Virtual Variables Without Instance
12 -----------------------------------------------
14 Virtual variables which belong in the virtual table can
15 be accessed without an object instance with the syntax
28 int total = A.count + B.count;
33 1. Structure By Reference calls Virtual
34 ---------------------------------------
46 a1.f (); // virtual call
47 a2.f (); // member call
50 For object a1, the call will be through the virtual table pointer to
51 function, while for a2 there will be a direct call of a2_mEmBeR function.
52 This is the right thing since except from the syntactical ease
53 of structures-by-reference, a1 is really a pointer and not a specific
54 instance known at compile-time.
57 2. Anonymous object call Constructor
58 ------------------------------------
60 In a local declaration the object name may be omitted if there is
61 a constructor call. For example:
73 In this case a temporary object of type A will be allocated, its
74 constructor will be called and then the destructor (if exists).
75 Then the object will die.
77 This is useful generally if an object is an alrgorithm which does
78 what it has to do at the contructor call.
84 In order to pass options to the lwc compiler, there is a special keyword
85 _lwc_config_. Example:
88 // stop compilation if version less than 0.6
91 // enable this debug flag which traces declarations
100 // C compatibility mode. Structures passed by value.
103 // inline all virtual tables by default
104 inline_virtual_tables;
106 // const virtual tables. Unable to set values to
107 // virtual variables however
108 const_virtual_tables;
110 // forward declaration of an explanatory section below
111 virtual_inheritance_declarations;
113 // auto comment the generated C code
115 /* File automatically generated by lwc compiler.
116 * still, Copyright (c) 1987, Stepan Trofimovits, though
117 * It's mine, it's mine, it's mine.
118 * NO WARRANTY FOR MERCHANTABILITY
119 * DO YOU KNOW OF ANY PRODUCT THAT GIVES WARRANTY FOR MERCHANTABILITY?
128 The _lwc_config_ reserved word must be followed by braces inside which
129 various options can be set. The option "version major,minor" for example
130 instructs the lwc compiler to terminate if its version is older than the
131 one required. See the file lwc_config.c from the lwc source for the
137 [B] Virtual Inheritance
138 =======================
140 Virtual inheritance is a complex case. Some of even the most advanced languages of
141 the world (Java, D, C#) don't support it. lwc tries to achieve a more lightweight
142 implementation without losing the power of virtual inheritance.
145 1. Avoid data in Virtual Base Classes if you can
146 ------------------------------------------------
148 Generally, virtual base classes without data members are virtuous,
149 both in lightweight C++ and in C++. Here is the wrong way and the
150 right way to implement virtual inheritance for a case where the virtual
151 functions will not need base-object's data members.
157 virtual int f1 () = 0;
158 virtual int f2 () = 0;
160 // this uses 'x' and calls 'f1' and 'f2'
164 class B : virtual A {
168 class C : virtual A {
177 virtual int f1 () = 0;
178 virtual int f2 () = 0;
181 class A0 : virtual A {
184 // uses 'x', 'f1', 'f2'
188 class B : virtual A {
192 class C : virtual A {
196 class D : A0, B, C {};
198 Remember that in C++ too. In lwc you can study the difference by looking at
199 the generated code. Moreover, in lwc in such cases the objects are position
200 independent and can be copied around.
203 2. Error about manual creation of virtual table
204 -----------------------------------------------
206 In the rare case of sequential virtual inheritance you may get this
207 message. It will happen in this dummy case for example:
213 class B : virtual A {};
215 //class X : virtual A {};
217 class C : virtual B {};
219 The downcast offset for B->C cannot be placed in the virtual table of A
220 because then the order of declaration would matter (B must be always
221 declared before in X in all files). For that lwc asks for the generation of
222 a virtual table for B (unlike C++ which takes the initiative to always
223 create a new virtual table for classes with virtual parents, to be prepared for
225 A virtual table can be manually created with "virtual;" or "inline virtual;":
227 class B : virtual A {
231 A virtual table will be generated anyway for B if it introduces new virtual