digitalmars.D.learn - Forwarding constructor arguments to super
- pillsy (19/19) Jun 12 2010 Is there a good way to forward constructor arguments to a superclass con...
 - bearophile (22/23) Jun 14 2010 This seems to work for simple situations, but maybe it doesn't work in m...
 
Is there a good way to forward constructor arguments to a superclass
constructor? I.e., if I have
something like this
class Foo {
  int _x;
  this (int x) { _x = x; }
}
class Bar : Foo {}
I'd like to able to have this just work:
auto bar = new Bar(3);
However, doing this means that DMD complains that Bar.this isn't callable with
an int argument. The
next thing I tried was
class Bar : Foo
{
  this (Args) (Args... args) { super(args): }
}
at which point DMD complains that constructor Bar.this conflicts with template
Bar.__ctor(Args...). I'm
using DMD 2.046, if it matters.
Thank you,
Pillsy
 Jun 12 2010
pillsy:Is there a good way to forward constructor arguments to a superclass constructor?This seems to work for simple situations, but maybe it doesn't work in more complex cases: import std.traits: ParameterTypeTuple; mixin template This() { this(ParameterTypeTuple!(super.__ctor) args) { super(args); } } class Foo { int _x; float _f; this(int x, float f) { this._x = x; this._f = f; } } class Bar : Foo { mixin This; } void main() { auto b = new Bar(10, 1.5); } Bye, bearophile
 Jun 14 2010








 
 
 
 bearophile <bearophileHUGS lycos.com>