Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
July 05, 2004 weak class conversions are LEGAL? | ||||
---|---|---|---|---|
| ||||
i'm liking D, but i came across this .... ugliness. class A { int x; } class B { int y; } int main() { A a=new A; B b=cast(B) a; return 0; } trying to access members of b will cause a memory access violation. why am i allowed to do this? |
July 05, 2004 Re: weak class conversions are LEGAL? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> i'm liking D, but i came across this .... ugliness.
>
> class A
> {
> int x;
> }
>
> class B
> {
> int y;
> }
>
> int main()
> {
> A a=new A;
> B b=cast(B) a;
> return 0;
> }
>
> trying to access members of b will cause a memory access violation.
>
> why am i allowed to do this?
>
>
When a cast fails, the result is null. So in this case you can catch the error with a simple test:
if(b === null)
printf("B is null");
or alternatively use the 'is' operator, which is the same as '===':
if(b is null)
...
|
July 05, 2004 Re: weak class conversions are LEGAL? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | Mike Parker wrote:
> When a cast fails, the result is null. So in this case you can catch the error with a simple test:
>
> if(b === null)
> printf("B is null");
>
> or alternatively use the 'is' operator, which is the same as '===':
>
> if(b is null)
> ...
Right, but it's pretty strange that it's not a compile-time error to cast an A to a B, even though B does not inherit A. The cast *has* to fail, so why is it even allowed?
-- andy
|
July 05, 2004 Re: weak class conversions are LEGAL? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | Andy Friesen wrote:
> Mike Parker wrote:
>> When a cast fails, the result is null. So in this case you can catch the error with a simple test:
>>
>> if(b === null)
>> printf("B is null");
>>
>> or alternatively use the 'is' operator, which is the same as '===':
>>
>> if(b is null)
>> ...
>
> Right, but it's pretty strange that it's not a compile-time error to cast an A to a B, even though B does not inherit A. The cast *has* to fail, so why is it even allowed?
True. The specifications are not very specific in this point (Heyheyhey,
what a pun!!!):
"D differs from C/C++ in another aspect of casts. Any casting of a class reference to a derived class reference is done with a runtime check to make sure it really is a proper downcast. This means that it is equivalent to the behavior of the dynamic_cast operator in C++."
A sentence should be added:
"Casts between references to unrelated classes references are illegal."
|
July 05, 2004 Re: weak class conversions are LEGAL? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | In article <ccalsv$c9v$1@digitaldaemon.com>, Mike Parker says... >When a cast fails, the result is null. So in this case you can catch the error with a simple test: But why should we have to add run-time code for something that could be detected at compile-time? This is a compile-time error. It should be reported at compile-time. Arcane Jill |
July 05, 2004 Re: weak class conversions are LEGAL? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | "Arcane Jill" <Arcane_member@pathlink.com> wrote in message news:ccb2t5$10tm$1@digitaldaemon.com... > In article <ccalsv$c9v$1@digitaldaemon.com>, Mike Parker says... > > >When a cast fails, the result is null. So in this case you can catch the error with a simple test: > > But why should we have to add run-time code for something that could be detected > at compile-time? This is a compile-time error. It should be reported at compile-time. It's a runtime error because one may be asking this question as part of the logic of the implementation of a template that selects one of several alternatives based on the type of a parameter. |
July 05, 2004 Re: weak class conversions are LEGAL? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote:
> "Arcane Jill" <Arcane_member@pathlink.com> wrote in message
> news:ccb2t5$10tm$1@digitaldaemon.com...
>
>>In article <ccalsv$c9v$1@digitaldaemon.com>, Mike Parker says...
>>
>>
>>>When a cast fails, the result is null. So in this case you can catch the
>>>error with a simple test:
>>
>>But why should we have to add run-time code for something that could be
>
> detected
>
>>at compile-time? This is a compile-time error. It should be reported at
>>compile-time.
>
>
> It's a runtime error because one may be asking this question as part of the
> logic of the implementation of a template that selects one of several
> alternatives based on the type of a parameter.
>
>
You're supposed to do that through template specialization, not run time checks :-), no?
|
July 05, 2004 Re: weak class conversions are LEGAL? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Horn | "Daniel Horn" <hellcatv@hotmail.com> wrote in message news:ccc6jd$2t30$1@digitaldaemon.com... > Walter wrote: > > It's a runtime error because one may be asking this question as part of the > > logic of the implementation of a template that selects one of several alternatives based on the type of a parameter. > You're supposed to do that through template specialization, not run time checks :-), no? Trying to do everything with specialization has been listed as one of the problems with C++ generic programming by some people who have made their reputations writing template libraries, so I am inclined to believe them <g>. |
Copyright © 1999-2021 by the D Language Foundation