c++ - c-style type cast and operator() -


question changed!

i use simple way hide enums local namespaces - enumeration inside of struct. goes this:

struct color {     enum type     {         red, green, black     };     type t_;     color(type t) : t_(t) {}     operator type () const {return t_;} private:   template<typename t>   operator t () const; }; 

operator t () protection implicit type casting. tried compile code gcc , keil:

color n; int a[9]; a[ (int)n ] = 1; 

gcc compiled no error (wich expected), keil gived me error: "invalid type conversion. operator () inaccessible".

so question is: compiler right?

i know c++11 enum class, isn't supported keil now

should reinterpret_cast (not c-style () cast) call type conversion operator?

no, reinterpret_cast used few dodgy types of conversions:

  • converting pointers integers , back
  • converting between pointers (and references) unrelated types

you shouldn't need cast @ use implicit conversion operator - have not prevented implicit conversion @ all. in c++11, if operator explicit, you'd need static_cast.

if you're stuck c++03, , want prevent implicit conversion allow explicit conversion, think sensible thing provide named conversion function.

update: question has changed, , asking c-style casting rather reinterpret_cast. should compile since conversion can done static_cast (including implicit conversions) can done c-style cast.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -