Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
August 02, 2002 cast | ||||
---|---|---|---|---|
| ||||
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? |
August 02, 2002 Re: cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dario | 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.
|
August 02, 2002 Re: cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dario | 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.
|
August 02, 2002 Re: cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Lewis | "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>. |
August 03, 2002 Re: cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dario | ...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 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? > > |
Copyright © 1999-2021 by the D Language Foundation