Implicit constructor call
Chris Nicholson-Sauls
ibisbasenji at gmail.com
Fri Jan 12 12:51:51 PST 2007
LeoD wrote:
> Hi,
> can constructors be called implicit in D? Example:
>
> class Foo
> {
> this(int i)
> {
>
> }
> }
>
> void test(Foo f)
> {
>
> }
>
> int main()
> {
> test(100); // Here Foo.this(100); should get called
>
> return 0;
> }
You can get this using typesafe variadic arguments:
# class Foo {
# this (int i) { }
# }
#
# void test (Foo f ...) { }
#
# int main () {
# test(100);
# return 0;
# }
See: http://digitalmars.com/d/function.html
Heading: Variadic Functions
Subheading: Typesafe Variadic Functions
>
> And why are there no templated constructors in D? Example:
>
> this(T)(T parameter)
> {
>
> }
Because constructors are not virtual... or at least that's the explanation given in
compiler errors with this. Seems more like just a special case. I was actually a little
bit disappointed myself, though the use cases seem to be pretty niche.
-- Chris Nicholson-Sauls
More information about the Digitalmars-d
mailing list