危险的运营商咒语?

时间:2012-02-05 12:51:54

标签: c++ operator-overloading

我在某处读到过载类型强制操作符存在风险。而且我在这里和那里都有一些段错误。那么,我有没有打扰黄蜂的窝?这是我的代码:

template <typename Wee>
struct xl_type_proxy_t {

   static_assert( 
       boost::mpl::or_<
       boost::is_same< Wee, WORD > ,
       boost::is_same< Wee, DWORD >
   >::value, "xl_type_proxy: should instantiate correctly" );

   Wee* where_to_drop;

   xl_type_proxy_t( Wee* wtd):
      where_to_drop( wtd )
   {}

   operator Wee() const {
      // Return stuff with state bits cleared
      //return xltypeNil;
      return (*where_to_drop) & 0xAFFF;
   }

   xl_type_proxy_t& operator=( Wee w )
   {
      // Save only the flags that were already in 
      // 'where_to_drop'.
      (*where_to_drop) = (*where_to_drop) & 0x5000;
      // And now over-write the rest
      (*where_to_drop) = (*where_to_drop) | w;
      return *this;
   }
};

...
xl_type_proxy_t< decltype(this->xl) > proxy_xl() 
{
      return xl_type_proxy_t<decltype(this->xl)>( &(this->xl));
}

... // In some places:
proxy_xl() = ... // Some numeric constant 

... // In some others:
if ( proxy_xl() == /* some numeric constant */ ) 
{
    ...
}

0 个答案:

没有答案