D - cast
- "Dario" <supdar yahoo.com> Aug 02 2002
- Russell Lewis <spamhole-2001-07-16 deming-os.org> Aug 02 2002
- "Walter" <walter digitalmars.com> Aug 02 2002
- Pavel Minayev <evilone omen.ru> Aug 02 2002
- "anderson" <anderson firestar.com.au> Aug 03 2002
When you use polymorphic classes, and you cast one type to another (in order to access a data-member), you use the following sintax: int dataMember2 = ( cast(DerivedClass) class_reference ).dataMember; Why the more intuitive sintax doesn't work? int dataMember2 = cast(DerivedClass)(class_reference).dataMember; It seems to me that class_reference is interpreted as a type (dmd alpha 0.35). I also fear that it has a different meaning, i.e. int dataMember2 = cast(DerivedClass)(class_reference.dataMember); which is a nonsense! IMO, the latter sintax is better, since it has less nested brackets and so it's more readable. Does anyone agree?
Aug 02 2002
Dario wrote:When you use polymorphic classes, and you cast one type to another (in order to access a data-member), you use the following sintax: int dataMember2 = ( cast(DerivedClass) class_reference ).dataMember; Why the more intuitive sintax doesn't work? int dataMember2 = cast(DerivedClass)(class_reference).dataMember; It seems to me that class_reference is interpreted as a type (dmd alpha 0.35). I also fear that it has a different meaning, i.e. int dataMember2 = cast(DerivedClass)(class_reference.dataMember); which is a nonsense! IMO, the latter sintax is better, since it has less nested brackets and so it's more readable. Does anyone agree?
The issue, of course, is operator precedence. The . operator has a higher precedence than does cast. Frankly, I would have preferred it the other way around - that cast would have higher precedence. However, it was not that way in C/C++, and one of Walter's stated goals is to make the C++ => D transition as easy as is practical. Thus, he has consistently refused to change the precedence tables (even when there was a very good case for it) or to change the meaning of keywords.
Aug 02 2002
"Russell Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3D4AEDF1.6070605 deming-os.org...Frankly, I would have preferred it the other way around - that cast would have higher precedence. However, it was not that way in C/C++, and one of Walter's stated goals is to make the C++ => D transition as easy as is practical. Thus, he has consistently refused to change the precedence tables (even when there was a very good case for it) or to change the meaning of keywords.
Yes, that is correct <g>.
Aug 02 2002
On Fri, 2 Aug 2002 20:26:00 +0200 "Dario" <supdar yahoo.com> wrote:I also fear that it has a different meaning, i.e. int dataMember2 = cast(DerivedClass)(class_reference.dataMember); which is a nonsense!
No, it isn't. C does it the same way! (only the cast keyword is missing). I guess it was done so to avoid confusion of C/C++ programmers.
Aug 02 2002
...but then changing a classed data member would look like this, classA A = cast(classX) (class.classBObject()); Which is just pushing the problem to another area, not getting rid of it. "Dario" <supdar yahoo.com> wrote in message news:aieij7$2q4n$1 digitaldaemon.com...When you use polymorphic classes, and you cast one type to another (in
to access a data-member), you use the following sintax: int dataMember2 = ( cast(DerivedClass) class_reference ).dataMember; Why the more intuitive sintax doesn't work? int dataMember2 = cast(DerivedClass)(class_reference).dataMember; It seems to me that class_reference is interpreted as a type (dmd alpha 0.35). I also fear that it has a different meaning, i.e. int dataMember2 = cast(DerivedClass)(class_reference.dataMember); which is a nonsense! IMO, the latter sintax is better, since it has less nested brackets and so it's more readable. Does anyone agree?
Aug 03 2002









"Walter" <walter digitalmars.com> 