struct inheritance need?

Christopher Wright dhasenan at gmail.com
Sat Dec 20 08:06:05 PST 2008


Denis Koroskin wrote:
> On Sat, 20 Dec 2008 16:46:44 +0300, Christopher Wright 
> <dhasenan at gmail.com> wrote:
> 
>> Sergey Gromov wrote:
>>> Thu, 18 Dec 2008 18:36:07 -0500, Christopher Wright wrote:
>>>
>>>> Weed wrote:
>>>>> Compile-time creation an object of class or (most likely wrong) 
>>>>> struct inheritance.
>>>>>
>>>>> I have prepared a distinct feature request and send it later
>>>> You aren't providing a use case, though. Why not show an example 
>>>> (actual code) of what you would do if you had this ability?
>>>  static invariant Identity = new Matrix([[1,0,0], [0,1,0], [0,0,1]]);
>>
>> Okay, do you want to access that matrix in a template? Is there a 
>> reason it can't be a struct?
> 
> Let's say, there is an IMatrix interface and its implementations, such 
> as SparseMatrix, RowMajor/ColumnMajorMatrix etc.
> 
> class IdentityMatrix : IMatrix
> {
>     public float at(int row, int col) { return (row == col) ? 1 : 0; }
> }

Score! We finally have a use case! I thought there would be a reasonable 
use case, but I couldn't come up with one off the top of my head.

The workaround would be to use a struct at compile time and convert to 
an appropriate class when necessary. This is a rather ugly workaround; 
it takes a fair bit of time at runtime and increases executable sizes (I 
don't think you could do a sparse matrix at compile time, for instance 
-- you would need dynamic allocation for that).


More information about the Digitalmars-d-learn mailing list