array of elements of various subclasses

Stewart Gordon smjg_1998 at yahoo.com
Sun Nov 7 17:52:59 PST 2010


On 07/11/2010 15:49, spir wrote:
<snip>
> And I'd like to know, as a possible workaround, if there is a way to save a variadic arg list:
> 	class C {
> 	    ??? xs;
> 	    this(X xs...) {
> 	        this.xs = xs;
> 	    }
> 	}

What exactly are you trying to do here?

If you declare it like that, you are declaring a function that takes a 
single object of class X, called by passing in the arguments of X's 
constructor.  And in any case, that code won't work, because the object 
may be allocated on the stack.

ISTM what you're really trying to do is this:

     class C {
         X[] xs;
         this(X[] xs...) {
             this.xs = xs.dup;
         }
     }

But if all you want to do is return an array, you might as well use a 
template function:

     T[] array(T)(T[] data...) {
         return data.dup;
     }

which you can instantiate either implicitly (for those cases where it 
works) or explicitly (for other cases).

Stewart.


More information about the Digitalmars-d-learn mailing list