Downcasting, opinions

Downcasting, opinions

Post by Siddhartha Se » Fri, 03 May 1996 04:00:00




>How do people out there feel about casts and downcasts?  I even
>went one step further to replace my casts with the static_cast<..>
>and dynamic_cast<..>...

>thanks and sorry for the long mail

Hello Paul:

     This is really *really* my opinion. The "argument against
dwncasting" runs something like this:

     a.    C++ is a statically typed language.
     b.    Hence, all error-checking is done statically because
           there is *no* dynamic error checking (a la Smalltalk/Java)
     c.    Downcasting (a dynamic event) makes the compiler believe you  
           and any errors will not be tracked a run-time.

     Looking at this set of arguments we could be justified in
     down-casting if there are error tracking dynamically.

     Continuing this set of arguments, dymanic_cast is a proper way
     provided you have checked the return value of the dynamic_cast and
     worked accordingly. Something like this ....

     class Base { /* something */ };
     class Der1 : public Base { .... };
     class Der2 : public Base { .... };

     Now you have:
     Base * xx;

     if ((dymanic_case <Der1 *> (xx)) != NULL)
     {
         // normal work with xx as Der1*
     }
     else
     {
        // some error handling ...
     }

     To *me* this is an acceptable way of doing things. That's why
     RTTI is there in the first place. RTTI is there to take care
     of dynamic events safely is a statically typed environment.

     I'll come with more arguments after hear some more points
     "from the other side".

     Have fun !!

--
_________________________________________________Siddhartha__Sen______

!Fax   : 558-8298 ! Res: 526-2812                                   !!
!Snail : PSI Data Systems, 19 Primrose Rd, Bangalore 560 025, INDIA !!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
".. don't accept what the elders have said or even what I have said,
without testing it first with your reason and common sense."
                        - Buddha's last sermon to his disciple Ananda
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Downcasting, opinions

Post by Kent To » Sat, 04 May 1996 04:00:00



Quote:>     This is really *really* my opinion. The "argument against
>dwncasting" runs something like this:

>     a.    C++ is a statically typed language.
>     b.    Hence, all error-checking is done statically because
>           there is *no* dynamic error checking (a la Smalltalk/Java)
>     c.    Downcasting (a dynamic event) makes the compiler believe you  
>           and any errors will not be tracked a run-time.
>     Looking at this set of arguments we could be justified in
>     down-casting if there are error tracking dynamically.

That depends what your goal is. If it is "to know that something
is wrong when it happens", then downcast is fine. But if it is
"to prevent things from going wrong", then downcast should be
avoided if it's avoidable. Just imagine when you derive a new
class from the base class then all the runtime type checking
code has to be modified and how error-prone this is!

Quote:>     To *me* this is an acceptable way of doing things. That's why
>     RTTI is there in the first place. RTTI is there to take care
>     of dynamic events safely is a statically typed environment.

Interesting point :-) To me RTTI is here because *sometimes*
it is useful (more specifically when downcast is unavoidable),
not because it should be used whenever it is usable.

Quote:>     Have fun !!

You too, pal! :-)

---
Kent Tong
Freeman Installer ==> http://www.netnet.net/users/freeman/


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Downcasting, opinions

Post by John E. Pott » Tue, 07 May 1996 04:00:00


: How do people out there feel about casts and downcasts?  I even
: went one step further to replace my casts with the static_cast<..>
: and dynamic_cast<..>...

Opinion:

I like your attitude.  We should always be concerned about down casts;
however, like all other features, there is a time when they are needed.
Going to the new style casts is a good idea.  At least they will be
much more visible.

For an example of sound unchecked downcasting which may help to soothe
your conscience, see C++2ed 8.3

Smile,
John


      [ about comp.lang.c++.moderated. First time posters: do this! ]