www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - weak class conversions are LEGAL?

reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
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?
Jul 04 2004
parent reply Mike Parker <aldacron71 yahoo.com> writes:
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) ...
Jul 04 2004
next sibling parent reply Andy Friesen <andy ikagames.com> writes:
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
Jul 04 2004
parent Norbert Nemec <Norbert.Nemec gmx.de> writes:
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."
Jul 05 2004
prev sibling parent reply Arcane Jill <Arcane_member pathlink.com> writes:
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
Jul 05 2004
parent reply "Walter" <newshound digitalmars.com> writes:
"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.
Jul 05 2004
parent reply Daniel Horn <hellcatv hotmail.com> writes:
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?
Jul 05 2004
parent "Walter" <newshound digitalmars.com> writes:
"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>.
Jul 05 2004