lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] Safe and consistent dereferencing and casting [Was: Code revie


From: Evgeniy Tarassov
Subject: Re: [lmi] Safe and consistent dereferencing and casting [Was: Code review: product editor]
Date: Fri, 13 Apr 2007 01:49:46 +0200

On 4/13/07, Vadim Zeitlin <address@hidden> wrote:

 Basically there are 2 options for implementing this function:

[...]

I strongly prefer the second form as I don't like provoking an exception
which we then immediately proceed to catch and ignore but Evgeniy thinks
that the first version is more clear (while for me it's exactly the
contrary). What do you think about this?

Oh, let me just slightly complete the code, since there are two
different checks:
- one for the pointer to be non-NULL
- another one for the pointer to be of the correct type

   template <typename T, typename U>
   T& safely_dereference_as(U *ptr)
   {
       if(!ptr)
           {
           fatal_error() << "Pointer could not be NULL" << LMI_FLUSH;
           }
       try
           {
           return dynamic_cast<T>(*ptr);
           }
       catch(std::bad_cast const& e)
           {
           fatal_error() << "Object is of a wrong type: " << e.what()
<< LMI_FLUSH;
           }
   }

or

   template <typename T, typename U>
   T& safely_dereference_as(U *ptr)
   {
       if(!ptr)
           {
           fatal_error() << "Pointer could not be NULL" << LMI_FLUSH;
           }
       T *p = dynamic_cast<T *>(ptr);
       if ( !p )
           {
           fatal_error() << "Object is of a wrong type: " << e.what()
<< LMI_FLUSH;
           }
       return *p;
   }

Anyway the question remains the same.

--
Best wishes,
Evgeniy Tarassov




reply via email to

[Prev in Thread] Current Thread [Next in Thread]