digitalmars.D - weak class conversions are LEGAL?
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> Jul 04 2004
- Mike Parker <aldacron71 yahoo.com> Jul 04 2004
- Andy Friesen <andy ikagames.com> Jul 04 2004
- Norbert Nemec <Norbert.Nemec gmx.de> Jul 05 2004
- Arcane Jill <Arcane_member pathlink.com> Jul 05 2004
- "Walter" <newshound digitalmars.com> Jul 05 2004
- Daniel Horn <hellcatv hotmail.com> Jul 05 2004
- "Walter" <newshound digitalmars.com> Jul 05 2004
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
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?
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
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
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
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
"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
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
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
detectedat 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.
checks :-), no?
Jul 05 2004
"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
logic of the implementation of a template that selects one of several alternatives based on the type of a parameter.
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









Norbert Nemec <Norbert.Nemec gmx.de> 