The practical use of dynamic_cast in reusable or generic code

Discussion in 'C++' started by Axter, May 9, 2005.

  1. Axter

    Axter Guest

    I'm wondering about the practical use of dynamic_cast in reusable or
    generic code.
    I'm currently working on a smart pointer that can be used on vector
    and other STL containers.
    See following link:
    http://code.axter.com/arr_ptr.h

    In above code, there's a function called clone_func_ptr_interface
    within the func_ptr_holder class.
    In this function, I need to cast from a base pointer to a derived
    pointer.
    I originally used dynamic_cast, but then I realized it would force
    anyone using the code to enable RTTI.

    I was also worried that using dynamic_cast would make my code less
    efficient.

    Considering that most projects don't have RTTI enabled, and that
    dynamic_cast could result in less efficient code, how practical is it
    to use dynamic_cast in reusable or generic code?
    Should it be avoided in this type of code, or should it be used
    regardless of how less reusable it makes the code?
    Axter, May 9, 2005
    #1
    1. Advertising

  2. In message <>, Axter
    <> writes
    >I'm wondering about the practical use of dynamic_cast in reusable or
    >generic code.
    >I'm currently working on a smart pointer that can be used on vector
    >and other STL containers.
    >See following link:
    >http://code.axter.com/arr_ptr.h
    >
    >In above code, there's a function called clone_func_ptr_interface
    >within the func_ptr_holder class.
    >In this function, I need to cast from a base pointer to a derived
    >pointer.
    >I originally used dynamic_cast, but then I realized it would force
    >anyone using the code to enable RTTI.


    If it isn't enabled, virtual functions won't work and you no longer have
    standard C++. If that's the case you are by definition off-topic here
    :-(
    >
    >I was also worried that using dynamic_cast would make my code less
    >efficient.


    Did you measure it?

    >
    >Considering that most projects don't have RTTI enabled,


    ???

    >and that
    >dynamic_cast could result in less efficient code, how practical is it
    >to use dynamic_cast in reusable or generic code?


    dynamic_cast is provided because it does something none of the other
    casts can do. If you need that functionality, never mind "practical",
    it's *necessary*. If that's the case, any workaround is likely to be
    even less efficient.

    >Should it be avoided in this type of code, or should it be used
    >regardless of how less reusable it makes the code?
    >

    Is it *needed*?

    --
    Richard Herring
    Richard Herring, May 12, 2005
    #2
    1. Advertising

  3. Axter

    Axter Guest

    Richard Herring wrote:
    > In message <>,

    Axter
    > <> writes
    > >I'm wondering about the practical use of dynamic_cast in reusable or
    > >generic code.
    > >I'm currently working on a smart pointer that can be used on vector
    > >and other STL containers.
    > >See following link:
    > >http://code.axter.com/arr_ptr.h
    > >
    > >In above code, there's a function called clone_func_ptr_interface
    > >within the func_ptr_holder class.
    > >In this function, I need to cast from a base pointer to a derived
    > >pointer.
    > >I originally used dynamic_cast, but then I realized it would force
    > >anyone using the code to enable RTTI.

    >
    > If it isn't enabled, virtual functions won't work and you no longer

    have
    > standard C++. If that's the case you are by definition off-topic here


    > :-(


    You don't need to have RTTI enable to have virtual functions working.
    The virtual functions work without RTTI enabled.

    > >
    > >I was also worried that using dynamic_cast would make my code less
    > >efficient.

    >
    > Did you measure it?
    >
    > >
    > >Considering that most projects don't have RTTI enabled,

    >
    > ???
    >
    > >and that
    > >dynamic_cast could result in less efficient code, how practical is

    it
    > >to use dynamic_cast in reusable or generic code?

    >
    > dynamic_cast is provided because it does something none of the other
    > casts can do. If you need that functionality, never mind "practical",


    > it's *necessary*. If that's the case, any workaround is likely to be
    > even less efficient.
    >
    > >Should it be avoided in this type of code, or should it be used
    > >regardless of how less reusable it makes the code?
    > >

    > Is it *needed*?
    >
    > --
    > Richard Herring


    The point is, that the code can work without dynamic_cast, but
    dynamic_cast could add extra runtime type safety.

    So what I'm weighing is the difference between adding extra runtime
    type safety and loosing efficiency as well as loosing reuse.
    Axter, May 13, 2005
    #3
  4. In message <>,
    Axter <> writes
    >
    >Richard Herring wrote:
    >> In message <>,

    >Axter
    >> <> writes
    >> >I'm wondering about the practical use of dynamic_cast in reusable or
    >> >generic code.
    >> >I'm currently working on a smart pointer that can be used on vector
    >> >and other STL containers.
    >> >See following link:
    >> >http://code.axter.com/arr_ptr.h
    >> >
    >> >In above code, there's a function called clone_func_ptr_interface
    >> >within the func_ptr_holder class.
    >> >In this function, I need to cast from a base pointer to a derived
    >> >pointer.
    >> >I originally used dynamic_cast, but then I realized it would force
    >> >anyone using the code to enable RTTI.

    >>
    >> If it isn't enabled, virtual functions won't work and you no longer

    >have
    >> standard C++. If that's the case you are by definition off-topic here

    >
    >> :-(

    >
    >You don't need to have RTTI enable to have virtual functions working.
    >The virtual functions work without RTTI enabled.


    Then you must mean something different from me by "RTTI". It doesn't
    matter, anyway. I'll rephrase the above.

    If dynamic_cast isn't enabled, you no longer have standard C++. If
    that's the case you are by definition off-topic here.

    >> >I was also worried that using dynamic_cast would make my code less
    >> >efficient.

    >>
    >> Did you measure it?
    >>
    >> >
    >> >Considering that most projects don't have RTTI enabled,

    >>
    >> ???
    >>
    >> >and that
    >> >dynamic_cast could result in less efficient code, how practical is

    >it
    >> >to use dynamic_cast in reusable or generic code?

    >>
    >> dynamic_cast is provided because it does something none of the other
    >> casts can do. If you need that functionality, never mind "practical",

    >
    >> it's *necessary*. If that's the case, any workaround is likely to be
    >> even less efficient.
    >>
    >> >Should it be avoided in this type of code, or should it be used
    >> >regardless of how less reusable it makes the code?
    >> >

    >> Is it *needed*?
    >>

    >
    >The point is, that the code can work without dynamic_cast, but
    >dynamic_cast could add extra runtime type safety.
    >
    >So what I'm weighing is the difference between adding extra runtime
    >type safety and loosing efficiency


    Then _weigh_ it. So far you're just _talking_ about it. Have you
    actually measured the efficiency loss?

    >as well as loosing reuse.


    Standard C++ provides dynamic_cast, so that's not a genuine issue.

    --
    Richard Herring
    Richard Herring, May 13, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    519
    Neredbojias
    Feb 17, 2007
  2. James Dow Allen

    Reusable source code

    James Dow Allen, Sep 6, 2010, in forum: C Programming
    Replies:
    55
    Views:
    1,365
    Eric Sosman
    Sep 14, 2010
  3. asit
    Replies:
    9
    Views:
    456
  4. Rodrigo Juarez

    Creating a reusable dropdownlist for use in formview

    Rodrigo Juarez, Feb 25, 2006, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    130
    Rodrigo Juarez
    Mar 2, 2006
  5. Chris Stankevitz
    Replies:
    33
    Views:
    1,449
    Chris Stankevitz
    Nov 19, 2011
Loading...

Share This Page