December 16, 2008 Re: distinguish between classes and structures | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | Kagamin пишет:
> Weed Wrote:
>
>> Without inheritance of structs many things are not possible, compared with C++.
>
> for example?
it is impossible to create the struct of the object which will be arbitrary size (raised by a user) + to be able to set compile-time + should work with the structures of its type.
this is mathematical matrix, for example
|
December 16, 2008 Re: distinguish between classes and structures | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | Frits van Bommel пишет:
> Weed wrote:
>> In C++, we had the problem - "slicing" objects.
>> In D this problem is solved inability to inherit from structs.
>> Without inheritance of structs many things are not possible, compared with C++.
>> Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value.
>>
>> like this:
>>
>> struct s1 {}
>> struct s2 : s1 {}
>>
>> s1 base;
>> s2 derr;
>>
>> s1* base_ptr = &derr; // ok
>> s1 val = derr; // error
>
> This is why:
> s1 val2 = *base_ptr; // error
Yes, right.
I do not understand that it is wrong?
>
> (And disallowing '*ptr' on struct pointers is not likely to find much
> support)
I am not saying do not let '* ptr'. Besides, now you can not do this just because there is no inheritance at all.
|
December 16, 2008 Re: distinguish between classes and structures | ||||
---|---|---|---|---|
| ||||
2008/12/16 Weed <resume755@mail.ru>:
> Frits van Bommel пишет:
>>
>> Weed wrote:
>>>
>>> In C++, we had the problem - "slicing" objects.
>>> In D this problem is solved inability to inherit from structs.
>>> Without inheritance of structs many things are not possible, compared
>>> with C++.
>>> Why, instead of the complete inability to inherit, just do not make
>>> impossible to up casting struct type by value.
>>>
>>> like this:
>>>
>>> struct s1 {}
>>> struct s2 : s1 {}
>>>
>>> s1 base;
>>> s2 derr;
>>>
>>> s1* base_ptr = &derr; // ok
>>> s1 val = derr; // error
>>
>> This is why:
>> s1 val2 = *base_ptr; // error
>
> I do not understand that it is wrong?
The problem is that base_ptr isn't actually pointing to an s1. So by dereferencing it and assigning to an s1 you just sliced it. Any extra data derr had is lost. Any method overrides it had are lost. So to avoid slicing you would need to disallow dereferencing struct pointers, or disallow pointing to a struct of a different type.
Disallowing dereferencing a struct pointer seems like it might be possible. But then again that's also kinda what class gives you already. And then you'd be left with no way to create a pointer that can be turned back into a value! That doesn't sound so good.
--bb
|
December 16, 2008 Re: distinguish between classes and structures | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter пишет: > 2008/12/16 Weed <resume755@mail.ru>: >> Frits van Bommel пишет: >>> Weed wrote: >>>> In C++, we had the problem - "slicing" objects. >>>> In D this problem is solved inability to inherit from structs. >>>> Without inheritance of structs many things are not possible, compared >>>> with C++. >>>> Why, instead of the complete inability to inherit, just do not make >>>> impossible to up casting struct type by value. >>>> >>>> like this: >>>> >>>> struct s1 {} >>>> struct s2 : s1 {} >>>> >>>> s1 base; >>>> s2 derr; >>>> >>>> s1* base_ptr = &derr; // ok >>>> s1 val = derr; // error >>> This is why: >>> s1 val2 = *base_ptr; // error >> I do not understand that it is wrong? > > The problem is that base_ptr isn't actually pointing to an s1. So by > dereferencing it and assigning to an s1 you just sliced it. This break garbage collection? Or what? > Any extra > data derr had is lost. Any method overrides it had are lost. So to > avoid slicing you would need to disallow dereferencing struct > pointers, or disallow pointing to a struct of a different type. > > Disallowing dereferencing a struct pointer seems like it might be > possible. But then again that's also kinda what class gives you > already. And then you'd be left with no way to create a pointer that > can be turned back into a value! That doesn't sound so good. |
December 16, 2008 Re: distinguish between classes and structures | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter пишет:
> 2008/12/16 Weed <resume755@mail.ru>:
>> Frits van Bommel пишет:
>>> Weed wrote:
>>>> In C++, we had the problem - "slicing" objects.
>>>> In D this problem is solved inability to inherit from structs.
>>>> Without inheritance of structs many things are not possible, compared
>>>> with C++.
>>>> Why, instead of the complete inability to inherit, just do not make
>>>> impossible to up casting struct type by value.
>>>>
>>>> like this:
>>>>
>>>> struct s1 {}
>>>> struct s2 : s1 {}
>>>>
>>>> s1 base;
>>>> s2 derr;
>>>>
>>>> s1* base_ptr = &derr; // ok
>>>> s1 val = derr; // error
>>> This is why:
>>> s1 val2 = *base_ptr; // error
>> I do not understand that it is wrong?
>
> The problem is that base_ptr isn't actually pointing to an s1. So by
> dereferencing it and assigning to an s1 you just sliced it. Any extra
> data derr had is lost. Any method overrides it had are lost. So to
> avoid slicing you would need to disallow dereferencing struct
> pointers, or disallow pointing to a struct of a different type.
>
> Disallowing dereferencing a struct pointer seems like it might be
> possible. But then again that's also kinda what class gives you
> already. And then you'd be left with no way to create a pointer that
> can be turned back into a value! That doesn't sound so good.
>
> --bb
Classes can not be created at compile time.
D, with its templates would do it perfectly for structs.
|
December 16, 2008 Re: distinguish between classes and structures | ||||
---|---|---|---|---|
| ||||
Posted in reply to Weed | Weed Wrote:
> it is impossible to create the struct of the object which will be arbitrary size (raised by a user) + to be able to set compile-time + should work with the structures of its type.
>
> this is mathematical matrix, for example
Matix? Easy.
class Matrix //can inherit from some generic base class
{
int[2] dims;
int[][] data;
//methods
}
|
December 16, 2008 Re: distinguish between classes and structures | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | Kagamin пишет:
> Weed Wrote:
>
>> it is impossible to create the struct of the object which will be arbitrary size (raised by a user) + to be able to set compile-time + should work with the structures of its type.
>>
>> this is mathematical matrix, for example
>
> Matix? Easy.
>
> class Matrix //can inherit from some generic base class
> {
> int[2] dims;
> int[][] data;
> //methods
> }
not just a matrix, see thread "struct inheritance need?" in this NG
|
Copyright © 1999-2021 by the D Language Foundation