[Issue 5056] Warning against virtual method call from constructor
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Oct 16 10:43:45 PDT 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5056
--- Comment #5 from Steven Schveighoffer <schveiguy at yahoo.com> 2010-10-16 10:43:10 PDT ---
(In reply to comment #4)
> The really bad thing is that D doesn't allow you to set up a sub class before
> calling the super constructor. Consider this:
>
> class Foo : Moo {
> int stuff;
> this(int stuff_) {
> this.stuff = stuff_;
> super();
> }
> override int get_stuff() {
> return stuff;
> }
> }
>
> This won't compile, because the compiler forces you to do the super ctor call
> _before_ setting up any members. This was probably thought as a feature to
> ensure "correct" construction, but it just doesn't work because the super ctor
> could call virtual functions. And it's really hard to work this around.
Hm... I just tried it, it does work. Were you thinking of something else?
example:
import std.stdio;
class A
{
this()
{
foo(1);
}
void foo(int x)
{
writeln("A.foo");
}
}
class B : A
{
int x;
this()
{
writeln("B.this");
x = 5;
super();
}
override void foo(int x)
{
writeln("B: ", this.x);
}
}
void main()
{
auto b = new B;
}
prints:
B.this
B: 5
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list