templates

Steven Schveighoffer schveiguy at yahoo.com
Tue Apr 20 03:27:10 PDT 2010


On Tue, 20 Apr 2010 02:01:23 -0400, Philippe Sigaud  
<philippe.sigaud at gmail.com> wrote:

> On Mon, Apr 19, 2010 at 22:58, Steven Schveighoffer  
> <schveiguy at yahoo.com>wrote:
>
>>
>> I'm not used to using interfaces in this way. What become the stored T
>>> values when you cast the classes into IW to construct your array? I
>>> suppose
>>> they're lost?
>>>
>>
>>
>> Not sure what you mean...
>>
>
> What if the class has some value in it? In your code:
>
> class WByVal(T) if (implementsW!T)
> {
>   this(T val) {this._t = val;}
>   private T _t;
>   int foo(int x)
>   {
>      return _t.foo(x);
>   }
> }
>
> What happens to _t when I cast a WByVal to a IW?

Nothing, it's still there.  Casting to an interface does nothing to the  
data.  An interface is simply an abstracted set of functions that can be  
used to access any object that implements that interface.

It's the same as casting to a base class.  Having a pointer to a base  
class, you don't have direct access to the data defined in the derived  
class, but the data is still there.

-Steve


More information about the Digitalmars-d-learn mailing list