digitalmars.D.learn - subclass instance construction
- spir (21/21) Nov 05 2010 Hello,
- Lars T. Kyllingstad (22/44) Nov 05 2010 This behaviour is by design. If the superclass has a constructor, but
- Tomek =?UTF-8?B?U293acWEc2tp?= (7/26) Nov 05 2010 "If no call to constructors via this or super appear in a constructor, a...
Hello, I have a weird bug related to class instance construction. My guess is that= it seems a superclass's this() is implicitely called (before the subclass'= s one, if any), even if overriden and without any use of "super". In my cas= e, I have schematically: =3D=3D=3D super class =3D=3D=3D this() { doThis(); } =3D=3D=3D sub class =3D=3D=3D this() { doFirst(); doThis(); } All happens as if doThis was performed _before_ doFirst... and performed ag= ain after doFirst! At least, that's what debug output shows, and it would c= orrectly explain the bug. Strange. How should I express what I mean? Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 05 2010
On Fri, 05 Nov 2010 10:54:11 +0100, spir wrote:Hello, I have a weird bug related to class instance construction. My guess is that it seems a superclass's this() is implicitely called (before the subclass's one, if any), even if overriden and without any use of "super". In my case, I have schematically: === super class === this() { doThis(); } === sub class === this() { doFirst(); doThis(); } All happens as if doThis was performed _before_ doFirst... and performed again after doFirst! At least, that's what debug output shows, and it would correctly explain the bug. Strange. How should I express what I mean?This behaviour is by design. If the superclass has a constructor, but you don't call it explicitly in the subclass constructor, the compiler inserts a call to super() at the beginning of the subclass constructor. Why is this required? Because the superclass needs to be properly constructed. If you don't call any of the superclass' constructors, it is most likely left in an inconsistent state. The solution is to call the superclass constructor explicitly, at the point where you want it to be called. This will give you the desired result: // superclass this() { doThis(); } // subclass this() { doFirst(); super(); // Make sure doThis() is called after doFirst() } -Lars
Nov 05 2010
spir napisaĆ:I have a weird bug related to class instance construction. My guess is that it seems a superclass's this() is implicitely called (before the subclass's one, if any), even if overriden and without any use of "super". In my case, I have schematically: === super class === this() { doThis(); } === sub class === this() { doFirst(); doThis(); } All happens as if doThis was performed before doFirst... and performed again after doFirst! At least, that's what debug output shows, and it would correctly explain the bug. Strange. How should I express what I mean?"If no call to constructors via this or super appear in a constructor, and the base class has a constructor, a call to super() is inserted at the beginning of the constructor." http://d-programming-language.org/class.html#Constructor -- Tomek
Nov 05 2010